Copy a String to Another using Character Arrays

In C, we can copy a string to another using character arrays by manually copying each character using a loop or utilizing built-in functions like strcpy(). Since C does not support direct assignment of character arrays, we must copy strings character by character.


Examples of Copying Strings

1. Copying a String Using a Loop

In this example, we will copy a string from one character array to another manually using a For loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char source[] = "Hello, World!";
    char destination[50]; // Large enough to hold the copied string
    int i;

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

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

    return 0;
}

Explanation:

  1. We declare a character array source[] containing the string “Hello, World!”.
  2. We create an empty destination[50] array large enough to store the copied string.
  3. We use a for loop to iterate through source, copying each character to destination.
  4. The loop stops when the null character '\0' is encountered, ensuring the copied string is valid.
  5. We manually add the null terminator destination[i] = '\0' at the end of the string.
  6. We print both the source and copied strings using printf().

Output:

Source String: Hello, World!
Copied String: Hello, World!

2. Copying a String Using strcpy()

In this example, we use the built-in strcpy() function from the string.h library to copy a string.

main.c

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

int main() {
    char source[] = "C Programming";
    char destination[50];

    // Using strcpy() to copy the string
    strcpy(destination, source);

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

    return 0;
}

Explanation:

  1. We include string.h to use the strcpy() function.
  2. We declare a character array source[] with the string “C Programming”.
  3. We create an empty destination[50] to hold the copied string.
  4. The strcpy(destination, source) function copies the contents of source into destination.
  5. We print both strings using printf().

Output:

Source String: C Programming
Copied String: C Programming

3. Copying a String Using while Loop

In this example, we will copy a string character by character using a while loop.

main.c

</>
Copy
#include <stdio.h>

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

    // Using while loop to copy string
    while (source[i] != '\0') {
        destination[i] = source[i];
        i++;
    }
    destination[i] = '\0'; // Add null terminator

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

    return 0;
}

Explanation:

  1. We declare source[] with the string “Copy Example”.
  2. An empty destination[50] array is allocated to hold the copied string.
  3. We initialize i = 0 and use a while loop to copy each character until we reach the null terminator.
  4. The null terminator is manually added at the end of destination.
  5. We print both strings using printf().

Output:

Source String: Arjun
Copied String: Arjun

Conclusion

In this tutorial, we explored different methods to copy a string into another character array in C:

  1. Using a loop: Manually copying characters one by one.
  2. Using strcpy(): Built-in function for easy copying.
  3. Using a while loop: Alternative method to copy strings dynamically.