Concatenate Two Strings in C
In C, we can concatenate (combine) two strings using various methods such as the strcat()
function from the string.h
library, manually appending characters using loops, or using pointer arithmetic.
In this tutorial, we will explore different ways to concatenate two strings with detailed explanations and examples.
Examples of String Concatenation in C
1. Using strcat()
Function to Concatenate Strings
In this example, we will use the built-in strcat()
function from the string.h
library to concatenate two strings.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";
// Concatenating str2 to str1
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
- We declare a character array
str1
with a sufficient buffer size (100 characters) to store the final concatenated string. - We declare another character array
str2
containing the string to be appended. - The
strcat()
function appendsstr2
to the end ofstr1
. - Finally, we print the concatenated string.
Output:
Concatenated String: Hello, World!
2. Concatenating Strings Using sprintf()
In this example, we will use the sprintf()
function to concatenate two strings into a buffer.
main.c
#include <stdio.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "Arjun!";
char result[100];
// Concatenating using sprintf
sprintf(result, "%s%s", str1, str2);
printf("Concatenated String: %s\n", result);
return 0;
}
Explanation:
- We declare two character arrays,
str1
andstr2
, containing the strings to concatenate. - A third character array,
result
, is created to store the concatenated output. - The
sprintf()
function is used to format and concatenatestr1
andstr2
intoresult
. - Finally, we print the concatenated string.
Output:
Concatenated String: Hello, Arjun!
3. Concatenating Strings Manually Using a Loop
In this example, we will manually append characters from the second string to the first string using a loop.
main.c
#include <stdio.h>
int main() {
char str1[100] = "Good ";
char str2[] = "Morning!";
int i, j;
// Find length of str1
for (i = 0; str1[i] != '\0'; i++);
// Append characters of str2 to str1
for (j = 0; str2[j] != '\0'; j++, i++) {
str1[i] = str2[j];
}
// Null terminate str1
str1[i] = '\0';
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
- We declare
str1
with enough buffer space and initialize it with “Good “. - We declare
str2
as “Morning!” to be appended. - A loop finds the length of
str1
to determine the starting position for appending. - Another loop appends characters from
str2
tostr1
one by one. - We manually insert the null character
'\0'
to terminate the string.
Output:
Concatenated String: Good Morning!
Conclusion
In this tutorial, we explored multiple ways to concatenate two strings in C:
- Using
strcat()
: A built-in function for string concatenation. - Using
sprintf()
: A formatted way to combine strings. - Manual Concatenation: Using loops to append characters.
Each method has its own use case. The strcat()
function is easy to use, while sprintf()
is flexible for formatted output. Manually appending characters gives full control over string manipulation.