0
0
CProgramBeginner · 2 min read

C Program to Concatenate Strings Without strcat Function

To concatenate strings without 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

Input"Hello", " World!"
OutputHello World!
Input"C", " Programming"
OutputC Programming
Input"", "Test"
OutputTest
🧠

How to Think About It

To join two strings without using 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

1
Find the length of the first string by moving through it until the null character is found.
2
Start copying characters from the second string to the position right after the end of the first string.
3
Continue copying until the null character of the second string is copied.
4
Add a null character at the end of the combined string to terminate it properly.
5
Print or return the concatenated string.
💻

Code

c
#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;
}
Output
Hello World!
🔍

Dry Run

Let's trace concatenating "Hello" and " World!" through the code

1

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))

2

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]

3

Add null terminator

Set str1[12] = '\0' to mark end of new string

4

Print result

Output is "Hello World!"

ijstr1[i]str2[j]
50' '' '
61'W''W'
72'o''o'
83'r''r'
94'l''l'
105'd''d'
116'!''!'
💡

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

Using pointers to concatenate
c
#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;
}
This method uses pointers for direct memory access, which can be faster and more elegant but may be harder for beginners.
Manual concatenation with array indexing and for loops
c
#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;
}
Using for loops with indexing is straightforward and clear, good for beginners but slightly longer code.

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.

ApproachTimeSpaceBest For
Indexing with while loopsO(n + m)O(1)Beginners, clarity
Pointer-based concatenationO(n + m)O(1)Performance and elegance
For loops with indexingO(n + m)O(1)Clear structure, beginners
💡
Always ensure the first string has enough space to hold the combined result before concatenating.
⚠️
Forgetting to add the null terminator '\0' after copying causes undefined behavior when printing.