Concatenate two string in Data structure by R4R Team

What is concatenation ?

Simply,Adding of two string is known as concatenation.

Example-
string_1 is "My"
string_2 is "website"
Then after concatenate both string :
"Mywebsite"
There are many possible ways to do this by C programming either you can direct concate or you can use predefined function that concate two string.

program-

#include< stdio.h>
int main()
{
char ch1[20],ch2[20];
int i=0,len=0;
//input of string
printf("Enter first string\n");
scanf("%s",&ch1);
printf("Enter second string\n");
scanf("%s",&ch2);
//Find length of first string
while(ch1[i++]!='')
len++;
//Now concatenate second string in first string
i=0;
while(ch2[i]!='')
{
ch1[len+i]=ch2[i];
i++;
}
ch1[len+i]='';
printf("Now first string is %s",ch1);
}


output-

Enter first string
My_program
Enter second string
_is_correct
Now first string is My_program_is_correct

-In this program, we take an input in two string(i.e., ch1 and ch2) bith having maximum size of 20.
-For Add second string in first string we follow following things
-Find length of first string.
-Run loop according to second string.
-Add each character of second string in first string at the last position.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!