C Program to Copy String Without Using strcpy()

String manipulation is a fundamental concept in C programming. Although the strcpy() function from the standard library is commonly used for copying strings, there are situations where you may want to implement this functionality yourself. This article explores three different C Program to Copy String Without Using strcpy() with explanations, example code, and outputs.

Prerequisites

Before diving into the solutions, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with strings and loops.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Using a Loop

1.1 Explanation

In this approach, we manually copy each character from the source string to the destination string using a loop. We continue until we encounter the null terminator '\0'.

1.2 Program

C
#include <stdio.h>

void copyString(char *destination, const char *source) {
    int i = 0;
    while (source[i] != '\0') {
        destination[i] = source[i];
        i++;
    }
    destination[i] = '\0';
}

int main() {
    char source[100], destination[100];
    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove the newline character introduced by fgets
    source[strcspn(source, "\n")] = '\0';

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

    return 0;
}

1.3 Output

C
Enter a string: Hello, tutcoach!
Copied string: Hello, tutcoach!

Solution 2: Using Pointers

2.1 Explanation

In this approach, we use pointers to copy the string. We traverse the source string using pointers and copy each character to the destination string until we reach the null terminator.

2.2 Program

C
#include <stdio.h>

void copyString(char *destination, const char *source) {
    while (*source) {
        *destination = *source;
        destination++;
        source++;
    }
    *destination = '\0';
}

int main() {
    char source[100], destination[100];
    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove the newline character introduced by fgets
    source[strcspn(source, "\n")] = '\0';

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

    return 0;
}

Output

C
Enter a string: Learning C
Copied string: Learning C

Solution 3: Using Recursion

3.1 Explanation

In this approach, we use recursion to copy the string. The base case is when the current character is the null terminator '\0', at which point we simply copy it and return. Otherwise, we copy the current character and call the function recursively for the next character.

3.2 Program

C
#include <stdio.h>

void copyString(char *destination, const char *source) {
    if (*source == '\0') {
        *destination = '\0';
    } else {
        *destination = *source;
        copyString(destination + 1, source + 1);
    }
}

int main() {
    char source[100], destination[100];
    printf("Enter a string: ");
    fgets(source, sizeof(source), stdin);

    // Remove the newline character introduced by fgets
    source[strcspn(source, "\n")] = '\0';

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

    return 0;
}

Output

C
Enter a string: Recursion is powerful
Copied string: Recursion is powerful

Conclusion

Copying a string is a fundamental operation in C programming. This article presented three different methods to achieve this task without using the strcpy() function: using a loop, using pointers, and using recursion. Each method has its own advantages and can be chosen based on the specific requirements of your application. By understanding and experimenting with these techniques, you can enhance your string manipulation skills and overall programming proficiency in C.