C Program to Copy String Without Using strcpy()

Updated on December 4, 2024
Copy string without using strcpy() header image

Introduction

In C programming, string manipulation is a fundamental concept, often involving copying strings from one variable to another. While the standard strcpy() function from the string.h library is commonly used for this, understanding how to manually copy strings is crucial for deepening your understanding of how strings work in C.

In this article, you will learn how to manually copy a string in C without using the strcpy() function. Explore various examples that demonstrate different methods to achieve this, enhancing your grasp of pointers, arrays, and character manipulation in C.

Manual String Copy Using a For Loop

Copying with Array Indexing

  1. Initialize the source string and an empty destination string of sufficient size.

  2. Use a for loop to iterate through the source string until the end is reached (indicated by a null character) and copy each character to the destination string.

    c
    #include <stdio.h>
    
    void copyString(char src[], char dst[]) {
        for (int i = 0; src[i] != '\0'; i++) {
            dst[i] = src[i];
        }
    }
    
    int main() {
        char source[] = "Hello World";
        char destination[20];
        copyString(source, destination);
        printf("Copied String: %s\n", destination);
        return 0;
    }
    

    This code snippet copies the string from source to destination using array indexing. Characters are copied one by one until the null character '\0' is encountered, indicating the end of the string.

Copying with Pointer Arithmetic

  1. Initialize the source string and an empty destination string.

  2. Use a loop to iterate through the source string, advancing the pointer until the null character is reached.

  3. Copy each character by dereferencing the source pointer and assigning it to the dereferenced destination pointer.

    c
    #include <stdio.h>
    
    void copyString(char *src, char *dst) {
        while (*src) {
            *dst++ = *src++;
        }
        *dst = '\0';  // Ensure the destination string is null-terminated
    }
    
    int main() {
        char source[] = "Hello World";
        char destination[20];
        copyString(source, destination);
        printf("Copied String: %s\n", destination);
        return 0;
    }
    

    In this example, the function copyString() uses pointers to navigate through the strings. The loop continues until the null character is found in the source string, copying each character to the destination. The destination string is explicitly null-terminated to prevent runtime errors.

Advanced String Copy Techniques

Using a While Loop with a Post-Increment

  1. Initialize your source and destination strings.

  2. Set up a while loop that runs as long as the value at the source pointer is not null.

  3. Copy the character and then advance both pointers.

    c
    #include <stdio.h>
    
    void copyStringV2(char *src, char *dst) {
        while (*dst++ = *src++);
    }
    
    int main() {
        char source[] = "Sample Text";
        char destination[20];
        copyStringV2(source, destination);
        printf("Destination String: %s\n", destination);
        return 0;
    }
    

    The expression *dst++ = *src++ not only copies the character from source to destination but also moves both pointers to the next character immediately after the assignment. This compact and efficient line performs the copy and pointer increment in a single statement.

Conclusion

Copying a string without using strcpy() in C provides valuable insights into string handling and pointer arithmetic. Starting from straightforward loop-based methods to more compact and advanced techniques using pointer increments, mastering these methods enhances your ability to manipulate low-level data effectively. Apply these string copying techniques to deepen your understanding of C and to handle scenarios where using standard library functions might not be allowed or preferred, ensuring your code remains flexible and robust.