Concatenate Two Strings using Character Arrays
To concatenate two strings using character arrays in C, we can use the strcat()
function from the string.h
library or manually copy characters from one array to another. Since character arrays are fixed in size, we must ensure the destination array has enough space to hold both strings.
In this tutorial, we will cover multiple ways to concatenate strings using examples.
Examples of String Concatenation
1. Concatenating Two Strings Using strcat()
In this example, we will use the standard library function strcat()
to concatenate two strings stored in character arrays.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
// Concatenating using strcat
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
- We declare two character arrays
str1
andstr2
.str1
has extra space to accommodate the concatenated result. - The
strcat()
function appendsstr2
to the end ofstr1
, ensuring the null terminator is properly placed. - We print the final concatenated string.
Output:
Concatenated String: Hello, World!
2. Concatenating Strings Manually Using a Loop
In this example, we will manually concatenate two character arrays by copying characters one by one using a while loop.
main.c
#include <stdio.h>
int main() {
char str1[50] = "Good ";
char str2[] = "Morning!";
int i, j;
// Finding the length of str1
i = 0;
while (str1[i] != '\0') {
i++;
}
// Copying str2 into str1
j = 0;
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Null terminate the final string
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
- We declare two character arrays,
str1
with extra space andstr2
containing the second string. - We determine the length of
str1
using awhile
loop to find the null terminator. - We then copy characters from
str2
intostr1
starting at the position found in the first step. - We append a null terminator at the end of
str1
to ensure it remains a valid C string. - The concatenated string is printed.
Output:
Concatenated String: Good Morning!
3. Concatenating Strings Using a Custom Function
In this example, we will create a custom function to concatenate two character arrays.
main.c
#include <stdio.h>
void concatenate(char str1[], char str2[]) {
int i = 0, j = 0;
// Move to the end of str1
while (str1[i] != '\0') {
i++;
}
// Append str2 to str1
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Null terminate the concatenated string
}
int main() {
char str1[50] = "Apple ";
char str2[] = "Banana!";
// Call the function to concatenate strings
concatenate(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
- We define a function
concatenate()
that takes two character arrays as parameters. - Inside the function, we find the length of
str1
to determine where to appendstr2
. - We copy characters from
str2
tostr1
one by one using a loop. - We ensure the final concatenated string is null-terminated.
- We call the function inside
main()
to concatenate and print the result.
Output:
Concatenated String: Apple Banana!
Conclusion
In this tutorial, we learned different ways to concatenate strings using character arrays in C:
- Using
strcat()
: Simplest way to concatenate strings. - Using a loop: Manually copying characters to concatenate strings.
- Using a custom function: Reusable approach for string concatenation.