This code combines two input strings entered by the user using the strcat() function from the string.h library.
#include <stdio.h>
#include <string.h> //strcat() function
int main()
{
char first_string[100];
char second_string[100];
printf("First String: ");
gets(first_string);
printf("Second String: ");
gets(second_string);
strcat(first_string, second_string);
printf("Combination: %s \n", first_string);
return 0;
}
Result:
First String: some
Second String: string
Combination: some string
Process returned 0 (0x0) execution time : 17.672 s
Press any key to continue.
Here's a line-by-line explanation:
#include <stdio.h>
#include <string.h> //strcat() function
int main()
{
char first_string[100];
char second_string[100];
The program includes the necessary header files, and defines two character arrays with a size of 100 each.
printf("First String: ");
gets(first_string);
printf("Second String: ");
gets(second_string);
The program prompts the user to enter two strings using printf() function and reads them using the gets() function.
Note: gets() function is considered unsafe and is deprecated in modern C. It is recommended to use fgets() instead.
strcat(first_string, second_string);
printf("Combination: %s \n", first_string);
return 0;
}
The program uses the strcat() function to concatenate the two strings, storing the result in the first_string variable. The concatenated string is then printed to the console using printf() function. Finally, the program returns 0 to indicate successful completion.