Introduction
Concatenating two strings means joining them end-to-end to form a single string. This guide will show you how to write a C program to concatenate two strings provided by the user.
Problem Statement
Create a C program that:
- Takes two strings as input from the user.
- Concatenates the second string to the end of the first string.
- Displays the concatenated string.
Example:
- Input:
- String1 = "Hello, "
- String2 = "World!"
- Output: Concatenated String = "Hello, World!"
Solution Steps
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>for standard input-output functions and#include <string.h>for string functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the two input strings and the concatenated string.
- Input the Strings: Use
getsorscanfto take input from the user for both strings. - Concatenate the Strings:
- Method 1: Use the
strcatfunction from thestring.hlibrary. - Method 2: Manually concatenate the strings by appending characters of the second string to the first string.
- Display the Concatenated String: Use
printfto display the concatenated string.
C Program Using strcat Function
#include <stdio.h>
#include <string.h>
int main() {
// Step 1: Declare variables to hold the two input strings
char str1[100], str2[100];
// Step 2: Prompt the user to enter the first string
printf("Enter the first string: ");
gets(str1); // Using gets to read the string including spaces
// Step 3: Prompt the user to enter the second string
printf("Enter the second string: ");
gets(str2); // Using gets to read the string including spaces
// Step 4: Concatenate the strings using strcat
strcat(str1, str2);
// Step 5: Display the concatenated string
printf("Concatenated string: %s\n", str1);
return 0; // Step 6: Return 0 to indicate successful execution
}
C Program Without Using strcat Function
#include <stdio.h>
int main() {
// Step 1: Declare variables to hold the two input strings
char str1[100], str2[100];
int i, j;
// Step 2: Prompt the user to enter the first string
printf("Enter the first string: ");
gets(str1); // Using gets to read the string including spaces
// Step 3: Prompt the user to enter the second string
printf("Enter the second string: ");
gets(str2); // Using gets to read the string including spaces
// Step 4: Find the length of the first string
i = 0;
while (str1[i] != '\0') {
i++;
}
// Step 5: Concatenate the strings manually by appending str2 to str1
j = 0;
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Add the null terminator at the end
// Step 6: Display the concatenated string
printf("Concatenated string: %s\n", str1);
return 0; // Step 7: Return 0 to indicate successful execution
}
Explanation
Using strcat Function
- strcat: The
strcatfunction from thestring.hlibrary is used to concatenatestr2to the end ofstr1. This function automatically handles the null terminator (\0).
Without Using strcat Function
- Manual Concatenation: The program manually concatenates the strings by appending each character from
str2to the end ofstr1. After appending all characters, it explicitly adds the null terminator to the end ofstr1.
Steps Explanation:
- Declare Variables: The
str1array stores the first input string, andstr2stores the second input string. - Input the Strings: The
getsfunction reads the input strings, including spaces, and stores them instr1andstr2. - Concatenate the Strings: Depending on the approach, either the
strcatfunction or a manual loop concatenatesstr2to the end ofstr1. - Display the Concatenated String: The program displays the concatenated string using the
printffunction.
Output Example
Example:
Enter the first string: Hello,
Enter the second string: World!
Concatenated string: Hello, World!
Another Example:
Enter the first string: C Programming
Enter the second string: is Fun!
Concatenated string: C Programming is Fun!
Conclusion
This C program demonstrates two methods to concatenate two strings: using the strcat function and manually concatenating character by character. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.