C Program to Concatenate two Strings without using Strcat
ADVERTISEMENTS
C program to concatenate two strings without using strcat.
In this article, you will learn to concatenate the two strings without using the strcat function, In this I will use the while loop and for loop to write this program.
Example
Concatenation result: hello this is my first program
You should know about the following topics in C programming to understand this program:
- C For Loop
- C While Loop
Source
// C Program to Concatenate two Strings without using Strcat
#include <stdio.h>
// It's the main function of the program
int main() {
char string_1[500] = "hello this is ";
char string_2[500] = "my first program";
int lenth_1;
// Step-1 Get the length of the first string
while (string_1[lenth_1] != '\0') {
lenth_1++;
}
// Step-2 Concatenate string_2 to string_1
for (int i = 0; string_2[i] != '\0'; i++, lenth_1++) {
string_1[lenth_1] = string_2[i];
}
// Step-3 Terminate the first string string_1
string_1[lenth_1] = '\0';
// Step-3 Final output of the program
printf("Concatenation result: ");
puts(string_1);
return 0;
}
Output
Concatenation result: hello this is my first program