Copy One String to Another in C

To copy one string to another in C, we use functions like strcpy() from the string.h library or manually copy characters using loops.


Examples of Copying Strings in C

1. Copying a String Using strcpy()

In this example, we use the strcpy() function from string.h to copy a source string into a destination string.

main.c

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

int main() {
    char source[] = "Hello, World!";
    char destination[50];

    // Copying string using strcpy()
    strcpy(destination, source);

    printf("Copied String: %s\n", destination);
    
    return 0;
}

Explanation:

  1. We include the string.h header file to use the strcpy() function.
  2. We declare a character array source and initialize it with “Hello, World!”.
  3. We declare another character array destination large enough to store the copied string.
  4. We use strcpy(destination, source) to copy the contents of source into destination.
  5. The printf() function prints the copied string.

Output:

Copied String: Hello, World!

2. Copying a String Using a Loop

Instead of using strcpy(), we can manually copy characters from one string to another using a loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char source[] = "Hello, Arjun!";
    char destination[50];
    int i;

    // Copying string using a loop
    for (i = 0; source[i] != '\0'; i++) {
        destination[i] = source[i];
    }
    destination[i] = '\0'; // Null-terminate the destination string

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

    return 0;
}

Explanation:

  1. We declare and initialize the character array source with “Manual Copy”.
  2. We declare another character array destination to store the copied string.
  3. We use a for loop to copy each character from source to destination until we reach the null character '\0'.
  4. After copying all characters, we manually append the null terminator '\0' to mark the end of the string.
  5. Finally, we print the copied string using printf().

Output:

Copied String: Hello, Arjun!

3. Copying a String Using strncpy()

We use strncpy() when we want to copy a limited number of characters from one string to another, ensuring buffer safety.

main.c

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

int main() {
    char source[] = "Hello, Ram!";
    char destination[15];

    // Copying first 9 characters using strncpy()
    strncpy(destination, source, sizeof(destination) - 1);
    destination[strlen(source)] = '\0'; // Ensuring null termination

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

    return 0;
}

Explanation:

  1. We declare and initialize the character array source with “Safe Copy”.
  2. We declare destination with a fixed size of 15 characters.
  3. We use strncpy(destination, source, sizeof(destination) - 1) to copy at most 9 characters.
  4. We manually append '\0' at the position of length of original string to ensure the string is properly null-terminated.
  5. Finally, we print the copied string using printf().

Output:

Copied String: Hello, Ram!

Conclusion

In this tutorial, we explored different methods for copying strings in C:

  1. strcpy(): The simplest way to copy strings using string.h.
  2. Loop Method: Manually copying character-by-character with a loop.
  3. strncpy(): A safer alternative to strcpy() for preventing buffer overflow.

Each method has its own use case, and choosing the right one depends on memory safety and the size of the destination buffer.