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
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Declare the Two Strings and Pointer Variables: Declare character arrays to store the two input strings and a pointer for traversal.
- Input the Two Strings: Use
gets
orfgets
to take input from the user. - 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.
- 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
andstr2
are declared to store the two input strings. - The pointers
ptr1
andptr2
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 thatgets
may be unsafe; for safer input, you can usefgets(str1, 100, stdin)
andfgets(str2, 100, stdin)
.
Step 4: Concatenate the Strings Using Pointers
ptr1
is initialized to point to the first character ofstr1
, andptr2
is initialized to point to the first character ofstr2
.- A
while
loop is used to moveptr1
to the end ofstr1
(where*ptr1
is'\0'
). - Another
while
loop is used to copy the characters fromstr2
to the end ofstr1
:- The loop continues until the null terminator (
'\0'
) ofstr2
is reached. - Each character from
str2
is copied to the location pointed to byptr1
. - Both
ptr1
andptr2
are incremented to move to the next positions instr1
andstr2
, respectively.
- The loop continues until the null terminator (
- After copying all characters, the concatenated string is null-terminated by assigning
'\0'
to the position pointed to byptr1
.
Step 5: Display the Concatenated String
- The program uses
printf
to display the concatenated string stored instr1
.
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.