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
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>for standard input-output functions and#include <string.h>for string functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the original string and the copied string.
- Input the String: Use
getsorscanfto take input from the user for the original string. - Copy the String:
- Method 1: Use the
strcpyfunction from thestring.hlibrary. - Method 2: Manually copy the string character by character.
- Display the Copied String: Use
printfto 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
strcpyfunction from thestring.hlibrary is used to copy the content ofstr1intostr2. This function automatically handles the null terminator (\0).
Without Using strcpy Function
- Manual Copy: The program manually copies each character from
str1tostr2using aforloop. After copying all characters, it explicitly adds the null terminator tostr2to mark the end of the string.
Steps Explanation:
- Declare Variables: The
str1array stores the original string, and thestr2array stores the copied string. - Input the String: The
getsfunction reads the input string, including spaces, and stores it instr1. - Copy the String: Depending on the approach, either the
strcpyfunction or a manual loop copies the content ofstr1intostr2. - Display the Copied String: The program displays the copied string using the
printffunction.
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.