0
0
CProgramBeginner · 2 min read

C Program to Copy String Without strcpy Function

You can copy a string in C without strcpy by using a loop to copy each character until the null terminator is reached, like while(src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0';.
📋

Examples

InputHello
OutputHello
InputC programming
OutputC programming
Input
Output
🧠

How to Think About It

To copy a string without using strcpy, think of copying each character one by one from the source string to the destination string until you reach the end, which is marked by the null character \0. This way, you manually replicate what strcpy does internally.
📐

Algorithm

1
Start with two strings: source and destination.
2
Initialize an index counter to zero.
3
Copy the character from source at the current index to destination.
4
Increase the index by one.
5
Repeat steps 3 and 4 until the character copied is the null terminator <code>\0</code>.
6
End the destination string with a null terminator.
💻

Code

c
#include <stdio.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50];
    int i = 0;

    while (src[i] != '\0') {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0';

    printf("Copied string: %s\n", dest);
    return 0;
}
Output
Copied string: Hello, World!
🔍

Dry Run

Let's trace copying "Hi" from src to dest through the code.

1

Initialize variables

src = "Hi", dest = uninitialized, i = 0

2

Copy first character

dest[0] = src[0] = 'H', i = 1

3

Copy second character

dest[1] = src[1] = 'i', i = 2

4

Copy null terminator

dest[2] = src[2] = '\0', loop ends

5

Result

dest = "Hi"

isrc[i]dest[i]
0HH
1ii
2\0\0
💡

Why This Works

Step 1: Copy characters one by one

The loop copies each character from source to destination until it finds the null character \0, which marks the string's end.

Step 2: Stop at null terminator

When the null character is copied, the loop stops to avoid copying extra memory.

Step 3: Add null terminator to destination

The destination string is properly ended with \0 so it behaves like a valid C string.

🔄

Alternative Approaches

Pointer increment
c
#include <stdio.h>

int main() {
    char src[] = "Hello";
    char dest[50];
    char *pSrc = src, *pDest = dest;

    while (*pSrc != '\0') {
        *pDest = *pSrc;
        pSrc++;
        pDest++;
    }
    *pDest = '\0';

    printf("Copied string: %s\n", dest);
    return 0;
}
Uses pointers to copy characters, which can be more efficient and idiomatic in C.
Do-while loop
c
#include <stdio.h>

int main() {
    char src[] = "Test";
    char dest[50];
    int i = 0;

    do {
        dest[i] = src[i];
    } while (src[i++] != '\0');

    printf("Copied string: %s\n", dest);
    return 0;
}
Uses a do-while loop to copy at least one character and stop after copying the null terminator.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs once for each character in the source string until the null terminator, so it is linear in the length of the string.

Space Complexity

No extra space besides the destination array is used; copying is done in-place character by character.

Which Approach is Fastest?

Pointer-based copying can be slightly faster due to direct memory access, but both loop and pointer methods are efficient for typical string sizes.

ApproachTimeSpaceBest For
Index loopO(n)O(1)Simple and clear copying
Pointer incrementO(n)O(1)Efficient and idiomatic C
Do-while loopO(n)O(1)Copying including empty strings
💡
Always add the null terminator \0 at the end of the destination string to avoid undefined behavior.
⚠️
Forgetting to add the null terminator \0 at the end of the copied string causes errors when printing or using the string.