String concatenation in C is the process of combining two or more strings into a single string. In C programming, strings are character arrays terminated by a null character ('\0'). The strcat() function, available in the string.h header file, is commonly used to concatenate one string to the end of another string.
For example, combining "Hello" and "World" produces "HelloWorld". In this tutorial, you will learn how to concatenate two strings in C using the strcat() function and without using built-in string functions, along with example programs and their outputs.
What Is String Concatenation in C?
String concatenation in C means combining two or more strings to form a single string.
C does not provide a dedicated string data type. Instead, strings are represented using character arrays terminated by a null character ('\0').
During concatenation, the characters of the second string are appended to the end of the first string, and the resulting string is terminated with a null character.
Syntax:
strcat(destination, source);
Here:
destination: The string at the end of which another string is appended.source: The string that needs to be appended.strcat(): The standard library function used to concatenate strings.
Example of String Concatenation
First String = "Hello"
Second String = "World"
Concatenated String = "HelloWorld"
Different Ways to Concatenate Two Strings
There are two commonly used methods:
| Method | Description |
|---|---|
Using strcat() | Uses the built-in string concatenation function. |
Without using strcat() | Uses a loop to copy characters manually. |
Let’s understand both methods with complete C programs.
C Program to Concatenate Two Strings
1. Without Using strcat()
We can concatenate two strings without using the strcat() function by manually copying the characters of the second string to the end of the first string.
C Program to concatenate string without using strcat()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[200], str2[100];
int i, j;
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove newline characters
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
// Find the end of the first string
i = strlen(str1);
// Copy second string to the end of first string
for (j = 0; str2[j] != '\0'; j++)
{
str1[i + j] = str2[j];
}
// Add null character
str1[i + j] = '\0';
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
Enter first string: Hello
Enter second string: World
Concatenated string: HelloWorld
Explanation of Above Program
Let’s understand the program step by step:
- Declare two character arrays: We declare
str1andstr2to store the two input strings. The first array has sufficient space to store the combined result. - Read the input strings: We use
fgets()to read both strings. Unlikescanf("%s"), it can also accept spaces within strings. - Remove newline characters: The
strcspn()function helps remove the newline character added byfgets(). - Find the length of the first string: The
strlen()function calculates the number of characters in the first string, excluding the null character. - Copy the second string: A
forloop copies each character ofstr2to the end ofstr1. - Add the null character: We add
'\0'at the end to ensure that the resulting character array is a valid C string. - Display the result: Finally, we print the concatenated string.
Dry Run of the Program
Consider the following input:
str1 = "Hello"
str2 = "World"
The length of the first string is 5.
Initially:
str1 = H e l l o \0
str2 = W o r l d \0
The loop copies the characters of str2 into the available positions of str1.
| Iteration | Character copied | Position | Updated string |
|---|---|---|---|
| 1 | W | 5 | HelloW |
| 2 | o | 6 | HelloWo |
| 3 | r | 7 | HelloWor |
| 4 | l | 8 | HelloWorl |
| 5 | d | 9 | HelloWorld |
After the loop, the null character is inserted at position 10.
Final Result:
HelloWorld
2. Using strcat()
The strcat() function is a predefined string-handling function available in the string.h header file.
It appends the contents of the source string to the destination string.
Syntax
strcat(destination, source);
Example of strcat()
char str1[20] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
Output:
HelloWorld
Note: In C, You can declare a character array without specifying its size when you initialize it with a string literal like we have done for str2[].
C Program to concatenate two strings using strcat()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[200], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove newline characters
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
// Concatenate two strings
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
Enter first string: Hello
Enter second string: World
Concatenated string: HelloWorld
Explanation of Above Program
- Declare two character arrays to store the input strings.
- Read both strings using
fgets(). - Remove the newline characters from both strings.
- Pass the two strings to the
strcat()function. - The function appends the second string to the end of the first string.
- Display the resulting string.