📝Beginner

Add or Concatenate Two Strings

Join two strings

Concatenate (join) two strings without using the strcat() function.

Program Code

string_concat.c
C
1#include <stdio.h>
2
3int main() {
4 char str1[100] = "Hello ";
5 char str2[] = "World!";
6
7 int i = 0, j = 0;
8
9 // Find end of str1
10 while (str1[i] != '\0') i++;
11
12 // Copy str2 to end of str1
13 while (str2[j] != '\0') {
14 str1[i] = str2[j];
15 i++;
16 j++;
17 }
18 str1[i] = '\0'; // Null terminate
19
20 printf("Concatenated: %s\n", str1);
21 return 0;
22}
Output

Concatenated: Hello World!