This is the example program to concatenate two given strings dynamically, i.e. at the run time. Concatenating simply means that appending one string to another.
Logic : Here the logic is simple that the program asks the user to enter the first string, succeeded by the second string to concatenate. The EOL (End Of Line) of the first string is taken and then the second string is added at this point, character by character. This loop will iterate till the EOL of the second line. After the end of the second string, an EOL will be added. Finally, the result will be displayed.
#include<stdio.h>
#include<conio.h>
void stcat(char str1[50], char str2[50]);
void main()
{
char str1[50], str2[50];
clrscr();
printf(“\n\n\t ENTER THE FIRST STRING…: “);
gets(str1);
printf(“\n\n\t ENTER THE FIRST STRING…: “);
gets(str2);
stcat(str1,str2);
printf(“\n\t THE CONCATENATED STRING IS…: “);
puts(str1);
getch();
}
void stcat(char str1[50], char str2[50])
{
int i = 0,len = 0;
while(str1[len]!=’\0′)
len++;
while(str2[i]!=’\0′)
{
str1[len] = str2[i];
i++;
len++;
}
str1[len] = ‘\0’;
}
Download exe and source code here.