C Program to Concatenate Two Strings Using Pointers

Introduction

Concatenating two strings involves appending the content of one string to the end of another. In C, this can be done using pointers to traverse the first string to its end, then copying the second string to the end of the first string. This guide will show you how to write a C program to concatenate two strings using pointers.

Example:

  • Input: First string "Hello", Second string "World"
  • Output: Concatenated string "HelloWorld"

Problem Statement

Create a C program that:

  • Takes two strings as input from the user.
  • Uses pointers to concatenate the second string to the end of the first string.
  • Displays the concatenated string.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Two Strings and Pointer Variables: Declare character arrays to store the two input strings and a pointer for traversal.
  3. Input the Two Strings: Use gets or fgets to take input from the user.
  4. Concatenate the Strings Using Pointers: Use pointers to traverse the first string to its end and then copy the second string to the end of the first string.
  5. Display the Concatenated String: Use printf to display the concatenated string.

C Program to Concatenate Two Strings Using Pointers

#include <stdio.h> int main() { // Step 2: Declare the two strings and pointer variables char str1[100], str2[100]; char *ptr1, *ptr2; // Step 3: Prompt the user to enter the two strings printf("Enter the first string: "); gets(str1); // Use fgets(str1, 100, stdin) if gets is deprecated printf("Enter the second string: "); gets(str2); // Use fgets(str2, 100, stdin) if gets is deprecated // Initialize pointers to the start of the strings ptr1 = str1; ptr2 = str2; // Step 4: Move the pointer to the end of the first string while (*ptr1 != '\0') { ptr1++; } // Copy the second string to the end of the first string while (*ptr2 != '\0') { *ptr1 = *ptr2; ptr1++; ptr2++; } *ptr1 = '\0'; // Null-terminate the concatenated string // Step 5: Display the concatenated string printf("Concatenated string: %s\n", str1); return 0; // Return 0 to indicate successful execution } 

Explanation

Step 2: Declare the Two Strings and Pointer Variables

  • The character arrays str1 and str2 are declared to store the two input strings.
  • The pointers ptr1 and ptr2 are declared to traverse the two strings.

Step 3: Input the Two Strings

  • The program prompts the user to enter the first and second strings using gets. Note that gets may be unsafe; for safer input, you can use fgets(str1, 100, stdin) and fgets(str2, 100, stdin).

Step 4: Concatenate the Strings Using Pointers

  • ptr1 is initialized to point to the first character of str1, and ptr2 is initialized to point to the first character of str2.
  • A while loop is used to move ptr1 to the end of str1 (where *ptr1 is '\0').
  • Another while loop is used to copy the characters from str2 to the end of str1:
    • The loop continues until the null terminator ('\0') of str2 is reached.
    • Each character from str2 is copied to the location pointed to by ptr1.
    • Both ptr1 and ptr2 are incremented to move to the next positions in str1 and str2, respectively.
  • After copying all characters, the concatenated string is null-terminated by assigning '\0' to the position pointed to by ptr1.

Step 5: Display the Concatenated String

  • The program uses printf to display the concatenated string stored in str1.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the first string: Hello Enter the second string: World Concatenated string: HelloWorld 

Conclusion

This C program demonstrates how to concatenate two strings using pointers. It covers basic concepts such as pointer manipulation, string traversal, and string handling, making it a useful example for beginners learning C programming.

Leave a Comment

Scroll to Top