I will list out the flaws in the code.
- "United" and "Front" are string constants, meaning they cannot be modified.
- strcat adds the second string to the first, as the first input is a constant string the operation is not possible.
- printf just need the 'str3'.
There are 2 ways to correct the code, a) get the str1 through scanf b) get str1 by strcpy
I have provided the second solution, also str1 & str3 will both have the final answer.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main( )
{
char *str1=malloc(20 * sizeof(char));
char *str2 = "Front" ;
char *str3 ;
if(str1){
strcpy(str1,"United");
str3 = strcat ( str1, str2 ) ;
printf ( "%s\n", str3 ) ;
free(str1);
}
else{
printf("malloc failure!\n");
}
}
With reference to the comments I have added a version without dynamic memory allocation:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main( )
{
char str1[20];
char *str2 = "Front" ;
char *str3 ;
strcpy(str1,"United");
str3 = strcat ( str1, str2 ) ;
printf ( "%s\n", str3 ) ;
}