C strcat

C strcat() function

Declaration:

char *strcat(char *str1,const char *str2)

This strcat() is used to append the copy of source string at the end of the destination string. if the first string is “uttar” and the second string is “pradesh” then after using strcat() the first string becomes the “uttarpradesh”. so in this process the null character is removed from the str1 string and str2 is added at the end of str1, but no changes in the str2 string or str2 remains unaffected. and the pointer return to the str1 string by the strcat().

Note : here programmer should already know there is enough space in the first string.

/* understand the strcat() */
#include<stdio.h>
#include<string.h>
void
main()
{
char str1[25],str2[20];
printf("Enter the first string");
gets(str1);
printf("\nEnter the second string");
gets(str2);
strcat(str1,str2);
printf("\nThe first string=%s \t and second string=%s \n",str1,str2);
strcat(str1,"newIndia");
printf("After now first string =%s",str1);
return 0;
}

output:
Enter the first string Ramesh
Enter the second string Kumar
The first string=rameshkumar and second string=kumar
After now first string =rameshkumarnewIndia

creation of this function

char *astract(char str1[],char str2[])
{
int i=0,j=0;
while(str1[i]!=’\0′)
{
i++; /* go to end of the first string */
}
while(str1[i]=str2[j++])
{
/* add second string at the end of first string */
}
return str1;
}

#include<stdio.h>
#include<string.h>
int main ()
{
char src[40], dest[40];
strcpy(src, "This is my source");
strcpy(dest, "This is my destination");
strcat(dest, src);
printf("After copied destination string : |%s|", dest);
return(0);
}

output:
After copied destination string : |This is my destinationThis is my source|