programming-examples/c/String/C Program to read two Strings _ Concatenate the Strings.c

43 lines
1.2 KiB
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* 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 i, 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 (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);
}
/*
*OUTPUT:
Enter the first string : San
Enter the second string: foundry
First string = San
Second string = foundry
Concatenated string = Sanfoundry
*/