Concatenate Two Strings In C Without Using Strcat

In the following program, the user would be asked to enter two strings, which would then be concatenated by the program. We did not use the standard library function strcat() for concatenation; instead, we wrote logic to append the second string at the end of the first string.

Concatenation of strings in C

/* C program to concatenate two strings without
 * using standard library function strcat()
#include <stdio.h>
int main()
   char str1[50], str2[50], i, j;
   printf("\nEnter first string: ");
   printf("\nEnter second string: ");
   /* This loop is to store the length of str1 in i
    * It just counts the number of characters in str1
    * You can also use strlen instead of this.
   for(i=0; str1[i]!='\0'; ++i); 
   /* This loop would concatenate the string str2 at
    * the end of str1
   for(j=0; str2[j]!='\0'; ++j, ++i)
   // \0 represents end of string
   printf("\nOutput: %s",str1);
   return 0;

As you can see, we entered two strings, and the program output concatenated both of them.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button