program to merge two strings in c

#include <stdio.h>
#include <string.h>

int main() {
    char str1[100], str2[100], result[200];

    printf("Enter the first string: ");
    gets(str1);

    printf("Enter the second string: ");
    gets(str2);

    strcpy(result, str1);
    strcat(result, str2);

    printf("The merged string is: %s\n", result);

    return 0;
}

Explanation

  1. Include the necessary header files: stdio.h for input/output operations and string.h for string manipulation functions.

  2. Define the main function which is the entry point of the program.

  3. Declare three character arrays: str1, str2, and result. These arrays will store the first string, second string, and the merged string, respectively. The sizes of these arrays are set to 100 characters, but you can adjust them as per your requirements.

  4. Prompt the user to enter the first string using the printf function.

  5. Use the gets function to read the first string from the user and store it in the str1 array. Note that gets is not a safe function to use as it may cause buffer overflow. It is recommended to use fgets instead.

  6. Prompt the user to enter the second string using the printf function.

  7. Use the gets function to read the second string from the user and store it in the str2 array.

  8. Use the strcpy function from the string.h library to copy the contents of str1 into result. This will initialize result with the first string.

  9. Use the strcat function from the string.h library to concatenate str2 to result. This will append the second string to the end of result.

  10. Print the merged string using the printf function.

  11. Return 0 to indicate successful program execution.