C Program to Concatenate Two Strings
Before writing a C program to concatenate two strings. Fist we should have clear understanding that what is a string. A string is a data type in C and it is a collection of characters grouped together. So it is a array of characters. A string declaration is shown as : char stringName [ stringSize ] .
For example : char name[25] ;
In C , compiler put a null ( ‘\0’) character at the end of string. So strings are always null terminated.

Now come to the point how to write C program to concatenate two strings . We can achieve our goals by two methods:
1 ) By using inbuilt C function strcat()
2) Without using strcat()

1 . Program to concatenate Two string without using strcat()
Solution 1:
#include <stdio.h> int main() { char str1[150]; char str2[150]; // to hold final ouptput char str3[150]; int i = 0, j = 0; printf("Enter First string: "); scanf("%s",str1); printf("Enter Second string: " ); scanf("%s",str2); // Inserting first string to str3 while (str1 [i] != '\0') { str3[j] = str1[i]; i++; j++; } // inserting str2 to str3 i = 0; while (str2[i] != '\0') { str3[j] = str2[i]; i++; j++; } str3[j] = '\0'; // to print final concatenated string printf("\nConcatenated string is : %s", str3); return 0; }
Output :

Solution 2:
/****************************************************************************** Concatenate two strings in C without using strcat function *******************************************************************************/ #include <stdio.h> int main() { char s1[100], s2[100], i, j; printf("\nEnter first string: "); scanf("%s",s1); printf("\nEnter second string: "); scanf("%s",s2); /* count lenght of s1 and store in variable i */ for(i=0; s1[i]!='\0'; ++i); /* concatenate string s2 to s1 */ for(j=0; s2[j]!='\0'; ++j, ++i) { s1[i]=s2[j]; } // \0 represents end of string s1[i]='\0'; printf("\nOutput: %s",s1); return 0; }
Output :

2 .Program to concatenate Two string using strcat()
Solution :
/****************************************************************************** Concatenate two strings in C without using strcat function *******************************************************************************/ #include <stdio.h> #include <string.h> int main() { char s1[100], s2[100], i, j; printf("\nEnter first string: "); scanf("%s",s1); printf("\nEnter second string: "); scanf("%s",s2); strcat(s1,s2); printf("Concatenated String: %s\n", s1); return 0; }
output :
