C Programming Code Examples C > Strings Code Examples C Program to read two Strings & Concatenate the Strings C Program to read two Strings & Concatenate the Strings This program takes two strings as input and concatenate them. 1. Take two strings as input and store them in the arrays string1 and string2 respectively. 2. Using for loop find the position of the last element of the array string1[]. Store that position in the variable pos. 3. Using another for loop add the elements of the array string2[] into the array string1[] starting from the obtained position. 4. Print the array string1[] as output. /* C program to read two strings and concatenate them, without using library functions. Display the concatenated string. */ #include <stdio.h> #include <string.h> void main() { char string1[20], string2[20]; int x, j, pos; /* Initialize the string to NULL values */ memset(string1, 0, 20); memset(string2, 0, 20); printf("Enter the first string : "); scanf("%s", string1); printf("Enter the second string: "); scanf("%s", string2); printf("First string = %s\n", string1); printf("Second string = %s\n", string2); /* Concate the second string to the end of the first string */ for (x = 0; string1[x] != '\0'; x++) { /* null statement: simply traversing the string1 */ ; } pos = x; for (j = 0; string2[j] != '\0'; x++) { string1[x] = string2[j++]; } /* set the last character of string1 to NULL */ string1[x] = '\0'; printf("Concatenated string = %s\n", string1); }