How To Concatenate Two Strings In C Without Using Strcat
In this tutorial, we are going to see how to concatenate two strings in C without using strcat. A string is a sequence of characters, enclosed in quotes (""), used to represent a string terminated by a null character ‘\0’ in C. If we try to concatenate two strings using the + operator, it will fail. The following example shows you how to concatenate two strings in C without using strcat.
How To Concatenate Two Strings In C Without Using Strcat
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
int i, j;
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
// To iterate the first string from beginning to end
for (i = 0; str1[i]!='\0'; i++);
// Concatenate Str2 with Str1
for (j = 0; str2[j]!='\0'; j++, i++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
printf("After the concatenation: %s\n", str1);
return 0;
}
Output:
Enter the first string: Hello Enter the second string: World After the concatenation: HelloWorld




