The string copy program in C is used to copy the contents of one string (source) into another string (destination). This involves iterating through each character of the source string and assigning it to the destination string until the null character ('\0') is encountered, indicating the end of the string.

Implementation of String Copy Program

Here’s a simple implementation of a program that copies one string to another in C:

#include <stdio.h> // Function to copy a string from source to destination void stringCopy(char *dest, const char *src) { while (*src != '\0') { // Loop until the end of the source string *dest = *src; // Copy each character from source to destination dest++; // Move to the next position in destination src++; // Move to the next position in source } *dest = '\0'; // Add null character to mark the end of the destination string } int main() { char source[100]; // Array to hold the source string char destination[100]; // Array to hold the copied string // Ask user for input printf("Enter a string: "); fgets(source, sizeof(source), stdin); // Read input including spaces source[strcspn(source, "\n")] = 0; // Remove the newline character if present // Copy the source string to the destination stringCopy(destination, source); // Display the result printf("Copied string: %s\n", destination); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is required for input and output functions.
  2. Function Definition:

    • void stringCopy(char *dest, const char *src): This function copies the source string to the destination string.
    • Parameters:
      • char *dest: Pointer to the destination string where the source string will be copied.
      • const char *src: Pointer to the source string that will be copied.
  3. While Loop:

    • The loop continues until the null character of the source string (*src != '\0') is reached.
    • Inside the loop:
      • Each character from the source string (*src) is copied to the destination string (*dest).
      • The pointers dest and src are incremented to move to the next character.
  4. Adding Null Character:

    • After copying all characters, a null character is added to the end of the destination string to properly terminate it.
  5. Main Function:

    • Two character arrays, source and destination, are declared to hold the strings.
    • The user is prompted to enter a string, which is read using fgets to include spaces.
    • The strcspn function is used to remove any newline character that might be present.
    • The stringCopy function is called to copy the source string to the destination string.
    • Finally, the copied string is displayed to the user.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc string_copy.c -o string_copy
  2. Execute the Program:

    ./string_copy
  3. Input Data: Enter a string when prompted.

Example Input/Output

Input:

Enter a string: Hello, World!

Output:

Copied string: Hello, World!

Conclusion

The string copy program in C demonstrates how to manually copy strings without using the built-in strcpy function from the C standard library. This understanding of how strings work, including their termination and character manipulation, is crucial for working with strings in C programming. It also helps to grasp concepts like pointers and memory management effectively.