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
getsorfgetsto 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
printfto 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
str1andstr2are declared to store the two input strings. - The pointers
ptr1andptr2are 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 thatgetsmay be unsafe; for safer input, you can usefgets(str1, 100, stdin)andfgets(str2, 100, stdin).
Step 4: Concatenate the Strings Using Pointers
ptr1is initialized to point to the first character ofstr1, andptr2is initialized to point to the first character ofstr2.- A
whileloop is used to moveptr1to the end ofstr1(where*ptr1is'\0'). - Another
whileloop is used to copy the characters fromstr2to the end ofstr1:- The loop continues until the null terminator (
'\0') ofstr2is reached. - Each character from
str2is copied to the location pointed to byptr1. - Both
ptr1andptr2are incremented to move to the next positions instr1andstr2, 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
printfto 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.