This is a 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 two different arrays.
2. Find the position of the last element of the first array and from that position keep on adding the elements of the second array.
Here is source code of the C program to read two strings & concatenate the strings. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to read two strings and concatenate them, without using
* library functions. Display the concatenated string.
char string1, string2;
int i, j, pos;
/* Initialize the string to NULL values */
memset(string1, 0, 20);
memset(string2, 0, 20);
printf("Enter the first string : ");
printf("Enter the second string: ");
printf("First string = %s\n", string1);
printf("Second string = %s\n", string2);
/* Concate the second string to the end of the first string */
for (i = 0; string1[i] != '\0'; i++)
/* null statement: simply traversing the string1 */
pos = i;
for (j = 0; string2[j] != '\0'; i++)
string1[i] = string2[j++];
/* set the last character of string1 to NULL */
string1[i] = '\0';
printf("Concatenated string = %s\n", string1);
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.
Enter the first string : San Enter the second string: foundry First string = San Second string = foundry Concatenated string = Sanfoundry
Sanfoundry Global Education & Learning Series – 1000 C Programs.