C Program to Concatenate Strings Without strcat Function
strcat, manually copy characters from the second string to the end of the first string using a loop, ensuring the first string has enough space and ends with a null character.Examples
How to Think About It
strcat, first find where the first string ends by locating its null character. Then, start copying each character from the second string to that position onward until the second string's null character is reached. Finally, add a null character at the end to mark the new end of the combined string.Algorithm
Code
#include <stdio.h> int main() { char str1[100] = "Hello"; char str2[] = " World!"; int i = 0, j = 0; // Find end of str1 while (str1[i] != '\0') { i++; } // Copy str2 to end of str1 while (str2[j] != '\0') { str1[i] = str2[j]; i++; j++; } // Null terminate str1[i] = '\0'; printf("%s\n", str1); return 0; }
Dry Run
Let's trace concatenating "Hello" and " World!" through the code
Find end of str1
i moves from 0 to 5 where str1[i] is '\0' (positions: H(0), e(1), l(2), l(3), o(4), '\0'(5))
Copy characters from str2
Copy ' ' at str2[0] to str1[5], then 'W' to str1[6], 'o' to str1[7], 'r' to str1[8], 'l' to str1[9], 'd' to str1[10], '!' to str1[11]
Add null terminator
Set str1[12] = '\0' to mark end of new string
Print result
Output is "Hello World!"
| i | j | str1[i] | str2[j] |
|---|---|---|---|
| 5 | 0 | ' ' | ' ' |
| 6 | 1 | 'W' | 'W' |
| 7 | 2 | 'o' | 'o' |
| 8 | 3 | 'r' | 'r' |
| 9 | 4 | 'l' | 'l' |
| 10 | 5 | 'd' | 'd' |
| 11 | 6 | '!' | '!' |
Why This Works
Step 1: Find the end of the first string
We loop through the first string until we find the null character '\0', which marks its end.
Step 2: Copy characters from the second string
Starting at the end of the first string, we copy each character from the second string one by one.
Step 3: Add the null terminator
After copying all characters, we add a null character '\0' to mark the end of the new combined string.
Alternative Approaches
#include <stdio.h> int main() { char str1[100] = "Hello"; char str2[] = " World!"; char *p = str1; while (*p != '\0') p++; char *q = str2; while (*q != '\0') { *p = *q; p++; q++; } *p = '\0'; printf("%s\n", str1); return 0; }
#include <stdio.h> int main() { char str1[100] = "Hello"; char str2[] = " World!"; int i, j; for (i = 0; str1[i] != '\0'; i++); for (j = 0; str2[j] != '\0'; j++) { str1[i + j] = str2[j]; } str1[i + j] = '\0'; printf("%s\n", str1); return 0; }
Complexity: O(n + m) time, O(1) space
Time Complexity
The program loops through both strings once, so time grows linearly with the total length of both strings.
Space Complexity
Concatenation is done in-place in the first string, so no extra space proportional to input size is needed.
Which Approach is Fastest?
Pointer-based concatenation can be slightly faster due to direct memory access, but all methods have the same linear time complexity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Indexing with while loops | O(n + m) | O(1) | Beginners, clarity |
| Pointer-based concatenation | O(n + m) | O(1) | Performance and elegance |
| For loops with indexing | O(n + m) | O(1) | Clear structure, beginners |
'\0' after copying causes undefined behavior when printing.