C Program to Copy a String Using Pointers

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

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. 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.
  3. Input the Source String: Use gets or fgets to take input from the user.
  4. Copy the String Using Pointers: Use pointers to traverse the source string and copy each character to the destination string.
  5. Display the Copied String: Use printf to 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 source and destination are declared to store the source and destination strings, respectively.
  • The pointers src_ptr and dest_ptr are 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 that gets may be unsafe; for safer input, you can use fgets(source, 100, stdin).

Step 4: Copy the String Using Pointers

  • src_ptr is initialized to point to the first character of the source string.
  • dest_ptr is initialized to point to the first character of the destination string.
  • A while loop 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_ptr is copied to the location pointed to by dest_ptr.
    • The pointers src_ptr and dest_ptr are then incremented to move to the next characters in the source and destination strings, respectively.
  • After copying all characters, the destination string is null-terminated by assigning '\0' to the position pointed to by dest_ptr.

Step 5: Display the Copied String

  • The program uses printf to 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.

Leave a Comment

Scroll to Top