C program to Concatenate two strings without using string library function strcat()

Implementation of strcat() function with for loop.
/* C program to join or append two strings without using string library function. */

#include
#include

void main()
{
 char str1[50],str2[50];
 int i,j,l1;
 
 printf("Enter String 1 :");
 gets(str1);
 printf("Enter String 2 :");
 gets(str2);
 
 /* find the length of string1 */
 for(i=0;str1[i]!='\0';i++)
 {}
 
 l1=i; /* l1= string 1 length */


 for(i=l1,j=0; str2[j]!='\0'; i++,j++)
 {
   str1[i]=str2[j];
 }
 
 str1[i]='\0';

 printf("\nAppended String 1 = %s\n",str1);

 getch();
}

Output:

Enter String 1 :hi
Enter String 2 :hello

Appended String 1 = hihello

If we are allowed to do the same program with string library function then following function is used to do the same task:

strcat(str1,str2);

The above line will append content of str2 after content of str1.