String concatenation is a fundamental operation in many programming tasks. In C, concatenating two strings can be done in various ways. This article presents three different C Program to Concatenate Two Strings with explanations, example code, and outputs.
Prerequisites
Before diving into the solutions, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with strings and loops.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Using a Loop
1.1 Explanation
In this approach, we manually copy each character of the second string to the end of the first string using a loop.
1.2 Program
#include <stdio.h>
void concatenate(char *str1, char *str2) {
int i = 0, j = 0;
// Find the end of the first string
while (str1[i] != '\0') {
i++;
}
// Copy the second string to the end of the first string
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}
int main() {
char str1[100], str2[50];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove the newline characters introduced by fgets
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
concatenate(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
1.3 Output
Enter first string: Hello,
Enter second string: World!
Concatenated string: Hello, World!
Solution 2: Using the strcat function
2.1 Explanation
The C standard library provides a built-in function strcat
to concatenate two strings. This function is defined in the string.h
header file.
2.2 Program
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[50];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove the newline characters introduced by fgets
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output
Enter first string: Learning C
Enter second string: is fun!
Concatenated string: Learning C is fun!
Solution 3: Using Pointers
3.1 Explanation
In this approach, we use pointers to concatenate two strings. This method is efficient and reduces the complexity of managing array indices.
3.2 Program
#include <stdio.h>
void concatenate(char *str1, char *str2) {
// Move the pointer to the end of the first string
while (*str1) {
str1++;
}
// Copy the second string to the end of the first string
while (*str2) {
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
}
int main() {
char str1[100], str2[50];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove the newline characters introduced by fgets
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
concatenate(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output
Enter first string: C programming
Enter second string: is powerful.
Concatenated string: C programming is powerful.
Conclusion
String concatenation is an essential operation in C programming. This article presented three different C Program to Concatenate Two Strings which is using a loop, using the strcat
function from the C standard library, and using pointers. Each method has its own advantages and can be chosen based on the specific requirements of your application. By understanding and experimenting with these techniques, you can efficiently manipulate strings and enhance your overall programming skills in C.