ProAnswers.org

Write a program to concatenate 2 strings in c

Write a program to concatenate 2 strings

#include /header file/
main()
{
char str1[40],str2[40];
int pos,count1,count2;

printf("

Enter the first string: “);
scanf(”%s",&str1); /enter string 1/

printf("

Enter the second string: “);
scanf(”%s",&str2); /enter string 2/

for(count1=0;str1[count1]!='\0';count1++)
{
;
}
pos=count1;
for(count1=pos,count2=0;str2[count2]!='\0';count1++)
{
str1[count1]=str2[count2++];
}
str1[count1]='\0';
printf("

Concatenated string is %s",str1); /print result/

#include

#include



#include



void main()



{



char name[50],fname[15],sname[15],lname[15];



int i,j,k;



clrscr();



printf(“First name:”);



gets(fname);



printf(“second name:”);



gets(sname);



printf(“last name:”);



gets(lname);



for(i=0;fname[i]!=‟\0‟;i++)



name[i]=fname[i];



name[i]=‟‟;



for(j=0;sname[j]!=‟\0‟;j++)



name[i+j+1]=sname[j];



name[i+j+1]=‟‟;



for(k=0;lname[k]!=‟\0‟;k++)



name[i+j+k+2]=lname[k];



name[i+j+k+2]=‟\0‟;



printf(“

”);

printf(“complete name after concatenation

”);

printf(“%s”, name);



getche();



}



[b]Output: [/b]



First name: ANANDA



Second name: MURUGAN



Last name: SELVARAJ



Complete name after concatenation



ANANDAMURUGANSELVARAJ