C Program to Copy a String Without strcpy()

C Program to Copy a String Without strcpy()

Copying a string is one of the most fundamental operations in C programming. Normally, this task can be done easily using the built-in library function strcpy(). However, writing your own code to copy strings without relying on strcpy() is an excellent way to understand how strings are stored and handled in memory.

When you copy a string manually, you gain practical knowledge of how arrays and pointers work, how the null terminator (\0) signals the end of a string, and how memory is managed in C. This exercise is also important because in many programming interviews and exams, you may be asked to implement such basic functions without using the standard library.

In this article, we will cover different ways to copy a string in C without using strcpy(). We will go through examples using loops, pointers, recursion, and sprintf(). Each method will be explained step by step so you can understand the logic behind it.

What Is A String?

A string in C is essentially an array of characters ending with the null character \0. Copying one string into another means taking every character from the source string and placing it into the destination string until the null terminator is reached, and then making sure the destination also ends with \0.

This problem is simple but very important. Without understanding how string copying works, it is difficult to master more advanced string manipulations such as concatenation, comparison, or substring extraction.

Program 1: Copying Using a Loop

This is the most straightforward method. We walk through each character in the source string and assign it to the destination string until the null terminator is found.

#include <stdio.h>
#include <string.h>

int main() {

    char source[100], destination[100];
    int i;

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove trailing newline added by fgets (if present)
    source[strcspn(source, "\n")] = '\0';

    // Copy characters one by one
    for (i = 0; source[i] != '\0'; i++) {
        destination[i] = source[i];
    }

    destination[i] = '\0';

    printf("Copied string: %s\n", destination);

    return 0;

}

This approach is easy to understand and shows exactly how characters are transferred from one array to another.

Program 2: Copying Using Pointers

With pointers, instead of using array indexes, we move through the strings directly. This method is cleaner and often faster.

#include <stdio.h>
#include <string.h>

int main() {

    char source[100], destination[100];
    char *src, *dest;

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove trailing newline added by fgets (if present)
    source[strcspn(source, "\n")] = '\0';

    src = source;
    dest = destination;

    while (*src != '\0') {

        *dest = *src;
        dest++;
        src++;

    }

    *dest = '\0';

    printf("Copied string: %s\n", destination);

    return 0;

}

Using pointers highlights how strings in C are really just memory locations that can be traversed.

Program 3: Copying Using Recursion

Recursion can also be used to copy a string. The function keeps copying one character at a time until it reaches the null terminator.

#include <stdio.h>
#include <string.h>

void copyString(char *src, char *dest) {

    *dest = *src;

    if (*src == '\0') return;

    copyString(src + 1, dest + 1);

}

int main() {

    char source[100], destination[100];

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove trailing newline added by fgets (if present)
    source[strcspn(source, "\n")] = '\0';

    copyString(source, destination);

    printf("Copied string: %s\n", destination);

    return 0;

}

This method is elegant but less practical in real-world programs, as recursion can be slower and use more memory compared to loops.

Program 4: Copying Using sprintf()

The sprintf() function is typically used for formatting strings, but it can also be used to copy strings directly.

#include <stdio.h>
#include <string.h>

int main() {

    char source[100], destination[100];

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove trailing newline added by fgets (if present)
    source[strcspn(source, "\n")] = '\0';

    sprintf(destination, "%s", source);

    printf("Copied string: %s\n", destination);

    return 0;

}

This method is concise and easy to use, but just like with concatenation, you need to ensure that the destination has enough space to hold the copied string.

Program 5: Copying Using sscanf()

The sscanf() function is usually used for reading formatted input, but it can also be used to copy a string from one variable to another.

#include <stdio.h>
#include <string.h>

int main() {

    char source[100], destination[100];

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Copy the string using sscanf
    sscanf(source, "%[^\n]", destination);

    printf("Copied string: %s\n", destination);

    return 0;

}

This method reads the entire input string into destination up to a newline. It is concise and avoids using sprintf() or loops, but you must ensure the destination buffer is large enough to hold the copied string.

Program 6: Copying Using memcpy()

The memcpy() function copies a specified number of bytes from one memory location to another. When copying strings, you need to include the null terminator to ensure the destination string is properly terminated.

#include <stdio.h>
#include <string.h>

int main() {

    char source[100], destination[100];

    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove trailing newline added by fgets (if present)
    source[strcspn(source, "\n")] = '\0';

    // Copy the string including the null terminator
    memcpy(destination, source, strlen(source) + 1);

    printf("Copied string: %s\n", destination);

    return 0;

}

This method is efficient and works at the memory level. Unlike sprintf(), memcpy() does not format the string—it simply copies the exact bytes. You must ensure the destination array is large enough to hold the entire string plus the null terminator.

FAQs

1. Which method is best for copying a string?
The loop method is simple and reliable. The pointer method is efficient, while sprintf() is concise. Recursion is more of a learning exercise.

2. Is sprintf() safe for string copying?
It works, but it can cause buffer overflow if the destination is too small. snprintf() is safer because it limits the number of characters written.

3. Why not just use strcpy()?
In real programs, strcpy() is fine, but writing your own function helps you understand how strings work in C.

4. Can I copy only part of a string?
Yes, you can modify the loop or pointer logic to stop after a certain number of characters, similar to how strncpy() works.

Conclusion

Copying a string without using strcpy() is a simple but essential exercise in C programming. It gives you a deeper understanding of how strings are managed in memory and teaches you the importance of the null terminator.

We explored four methods: using loops, using pointers, using recursion, and using sprintf(). While in practice you would normally rely on library functions, mastering these approaches will make you a stronger programmer and prepare you for technical interviews.

References & Additional Resources

A curated collection of textbooks, tutorials, and documentation for learning string copying, pointers, and C string functions.

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – The classic reference covering strings, pointers, arrays, and essential C programming concepts.
  2. GeeksforGeeks: Copy a String Without strcpy – Demonstrates methods to copy strings manually in C, without using strcpy().
  3. Tutorialspoint: C Strings – Overview of string declaration, initialization, and operations in C.
  4. Cprogramming.com: Pointers Tutorial – Beginner-friendly guide on using pointers for array and string manipulation.
  5. cplusplus.com: sprintf() – Reference for the sprintf() function for formatting and storing strings in C.
  6. ISO C Standard Library – Official reference for standard C string functions, including copying, concatenation, and length calculation.
Scroll to Top