Introduction
Copying a string involves duplicating the content of one string into another. In C, this can be done using pointers to traverse the source string and copy each character to the destination string. This guide will show you how to write a C program to copy a string using pointers.
Example:
- Input: Source string
"Hello" - Output: Destination string
"Hello"
Problem Statement
Create a C program that:
- Takes a source string as input from the user.
- Uses pointers to copy the source string to a destination string.
- Displays the copied string.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Declare the Source and Destination Strings and Pointer Variables: Declare character arrays to store the source and destination strings, along with pointer variables for traversal.
- Input the Source String: Use
getsorfgetsto take input from the user. - Copy the String Using Pointers: Use pointers to traverse the source string and copy each character to the destination string.
- Display the Copied String: Use
printfto display the destination string.
C Program to Copy a String Using Pointers
#include <stdio.h>
int main() {
// Step 2: Declare the source and destination strings and pointer variables
char source[100], destination[100];
char *src_ptr, *dest_ptr;
// Step 3: Prompt the user to enter the source string
printf("Enter the source string: ");
gets(source); // Use fgets(source, 100, stdin) if gets is deprecated
// Initialize pointers to the start of the source and destination strings
src_ptr = source;
dest_ptr = destination;
// Step 4: Copy the string using pointers
while (*src_ptr != '\0') {
*dest_ptr = *src_ptr; // Copy character from source to destination
src_ptr++; // Move source pointer to the next character
dest_ptr++; // Move destination pointer to the next position
}
*dest_ptr = '\0'; // Null-terminate the destination string
// Step 5: Display the copied string
printf("Copied string: %s\n", destination);
return 0; // Return 0 to indicate successful execution
}
Explanation
Step 2: Declare the Source and Destination Strings and Pointer Variables
- The character arrays
sourceanddestinationare declared to store the source and destination strings, respectively. - The pointers
src_ptranddest_ptrare declared to traverse the source and destination strings.
Step 3: Input the Source String
- The program prompts the user to enter the source string using
gets. Note thatgetsmay be unsafe; for safer input, you can usefgets(source, 100, stdin).
Step 4: Copy the String Using Pointers
src_ptris initialized to point to the first character of the source string.dest_ptris initialized to point to the first character of the destination string.- A
whileloop is used to traverse the source string:- The loop continues until the null terminator (
'\0') is encountered. - Inside the loop, the character pointed to by
src_ptris copied to the location pointed to bydest_ptr. - The pointers
src_ptranddest_ptrare then incremented to move to the next characters in the source and destination strings, respectively.
- The loop continues until the null terminator (
- After copying all characters, the destination string is null-terminated by assigning
'\0'to the position pointed to bydest_ptr.
Step 5: Display the Copied String
- The program uses
printfto display the destination string.
Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example Output:
Enter the source string: Hello
Copied string: Hello
Conclusion
This C program demonstrates how to copy a string using pointers. It covers basic concepts such as pointer manipulation, string handling, and working with null-terminated strings. This example is useful for beginners learning how to work with pointers and strings in C programming.