11.16.2011

Concatenate two strings

Q. Write a program to concatenate two strings without using the strcat() function.

Ans.

/*program to concatenate two strings without using the strcat() function*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int x=0,y=0,z=0;
 char str1[20],str2[20];
 char add[40];
 printf("Enter first string : ");
 gets(str1);
 printf("\nEnter second string : ");
 gets(str2);
 while(str1[x]!=NULL)
 {
   add[z]=str1[x];
   x++;
   z++;
 }
 while(str2[y]!=NULL)
 {
   add[z]=str1[y];
   y++;
   z++;
 }
 add[z]=NULL;
 puts(add);
 getch();
 return 0;
}

/***************Output******************

Enter first string : Input
Enter second string : Device
InputDevice


*****************************************/

No comments:

Post a Comment