C Program to Copy One String to Another

Introduction

Copying a string involves duplicating the content of one string into another. This guide will show you how to write a C program to copy one string to another string using both a library function and manually, without using any library functions.

Problem Statement

Create a C program that:

  • Takes a string as input from the user.
  • Copies the content of this string into another string.
  • Displays the copied string.

Example:

  • Input: String1 = "Hello, World!"
  • Output: String2 = "Hello, World!"

Solution Steps

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the original string and the copied string.
  4. Input the String: Use gets or scanf to take input from the user for the original string.
  5. Copy the String:
  • Method 1: Use the strcpy function from the string.h library.
  • Method 2: Manually copy the string character by character.
  1. Display the Copied String: Use printf to display the copied string.

C Program Using strcpy Function

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

int main() {
    // Step 1: Declare variables to hold the original and copied strings
    char str1[100], str2[100];

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Copy the string using strcpy
    strcpy(str2, str1);

    // Step 4: Display the copied string
    printf("Copied string: %s\n", str2);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

C Program Without Using strcpy Function

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the original and copied strings
    char str1[100], str2[100];
    int i;

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Copy the string manually character by character
    for (i = 0; str1[i] != '\0'; i++) {
        str2[i] = str1[i];
    }
    str2[i] = '\0';  // Add the null terminator at the end of the copied string

    // Step 4: Display the copied string
    printf("Copied string: %s\n", str2);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Using strcpy Function

  • strcpy: The strcpy function from the string.h library is used to copy the content of str1 into str2. This function automatically handles the null terminator (\0).

Without Using strcpy Function

  • Manual Copy: The program manually copies each character from str1 to str2 using a for loop. After copying all characters, it explicitly adds the null terminator to str2 to mark the end of the string.

Steps Explanation:

  1. Declare Variables: The str1 array stores the original string, and the str2 array stores the copied string.
  2. Input the String: The gets function reads the input string, including spaces, and stores it in str1.
  3. Copy the String: Depending on the approach, either the strcpy function or a manual loop copies the content of str1 into str2.
  4. Display the Copied String: The program displays the copied string using the printf function.

Output Example

Example:

Enter a string: Hello, World!
Copied string: Hello, World!

Another Example:

Enter a string: C Programming is fun!
Copied string: C Programming is fun!

Conclusion

This C program demonstrates two methods to copy one string to another: using the strcpy function and manually copying character by character. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top