Concatenating Two Strings in C
Last Updated :
14 Aug, 2024
Write a C program to concatenate two strings.
String concatenation is the process of joining two strings end-to-end to form a single string.
Examples
Input: str1 = “Hello”, str2 = “World”
Output: “HelloWorld”
Explanation: Joining “Hello” and “World” results in “HelloWorld”.
Input: str1 = “Good”, str2 = “Morning”
Output: “GoodMorning”
Explanation: Joining “Good” and “Morning” results in “GoodMorning”
String Concatenation in C
In C, strcat() function appends the copy of the given source string to another destination string with a NULL character at the end. It is defined inside <string.h> header file in C. It means that the source string characters will be copied to the end of the destination string.
Syntax of strcat()
char *strcat(char *dest, const char *src);
where, src is the string to be appended and dest is the string where src is to be appended.
C Program to Concatenating Two Strings
C
// C Program to concatenate two strings using strcat()
#include <stdio.h>
#include <string.h>
int main() {
char dest[100] = "Geeksfor";
char src[100] = "Geeks";
// Concatenate src to dest
strcat(dest, src);
printf("Concatenated String: %s\n", dest);
return 0;
}
OutputConcatenated String: GeeksforGeeks
Time Complexity: O(n + m) where n and m are the lengths of src and dest respectively.
Auxiliary Space: O(n)
Note: The destination string should be large enough to accommodate the concatenated data to avoid errors.
Different Ways to Concatenate Two Strings in C
Apart from the above straightforward method, there are also other methods in C to concatenate two strings:
1. Concatenating Two Strings Manually Using Loops
We can also concatenate two strings manually using loops that traverse the whole source string and copies each character to the destination string one by one.
- Find the length of destination string dest.
- Loop through each character of src and insert it at the end of the dest.
- Null-terminate the concatenated dest string.
Program to Concatenate Two Strings Manually Using Loops
C
// C Program to concatenate two strings manually using loops
#include <stdio.h>
void concatStr(char *dest, const char *src) {
int length, j;
// Find the length of the dest string
for (length = 0; dest[length] != '\0'; length++);
// Append the src string to the dest string
for (j = 0; src[j] != '\0'; j++, length++) {
dest[length] = src[j];
}
// Null-terminate the concatenated dest string
dest[length] = '\0';
}
int main() {
char dest[100] = "Geeksfor";
char src[100] = "Geeks";
// Call the concatStr function
concatStr(dest, src);
printf("Concatenated String: %s\n", dest);
return 0;
}
OutputConcatenated String: GeeksforGeeks
Time Complexity: O(n + m) where n and m are the lengths of src and dest respectively.
Auxiliary Space: O(n)
When to Use?
Use this method when you need full control over the concatenation process and no dependencies on library functions or you need to do some processing while concatenating.
2. Concatenating Two Strings Using sprintf() Function
The sprintf() function prints the given formatted string to the given string buffer instead of the output console. This method can be used to concatenate two strings by using format specifiers strings.
Syntax of sprintf()
sprintf(buffer, "{formatted_string}", args....);Program to Concatenate Two Strings Using sprintf() Function
- Create a temporary string temp.
- Print both src and dest to the temp string.
- Copy the contents of the temp string to dest string.
C
// C Program to demonstrate the concatStr function
// using sprintf
#include <stdio.h>
#include <string.h>
void concatStr(char *dest, const char *src) {
// Temporary string buffer
char temp[200];
// sprintf call with temp as buffer, and formatted
// string such that it concatenates the src to
// the dest string and store it in temp
sprintf(temp, "%s%s", dest, src);
// Seplacing the contents of the dest
strcpy(dest, temp);
}
int main() {
char dest[100] = "Geeksfor";
char src[100] = "Geeks";
// Call the concatStr function
concatStr(dest, src);
printf("Concatenated String: %s\n", dest);
return 0;
}
OutputConcatenated String: GeeksforGeeks
Time Complexity: O(n + m) where n and m are the lengths of src and dest respectively.
Space Complexity: O(1)
When to use?
This method should be used for complex formatting with concatenation as it supports the C formatted string input.
3. Concatenating Two Strings Using memmove()
The memmove() function from the C standard library can be used to concatenate strings by copying the block of memory of second string to the end of the first string memory.
Syntax of memmove()
memmove(void *dest, const void *src, numBytes);
where numBytes tell the number of bytes to be copied.
Program to Concatenate Two Strings Using memmove()
C
// C Program to demonstrate the concatStr function using memmove
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void concatStr(char *dest, const char *src) {
// Find the length of the first string
size_t len1 = strlen(dest);
size_t len2 = strlen(src);
// Move the second string to the end of the first string
// +1 to include the null terminator
memmove(dest + len1, src, len2 + 1);
}
int main() {
char dest[100] = "Geeksfor";
char src[100] = "Geeks";
// Call the concatStr function
concatStr(dest, src);
printf("Concatenated String: %s\n", dest);
return 0;
}
OutputConcatenated String: GeeksforGeeks
Time Complexity: O(n + m) where n and m are the lengths of src and dest respectively.
Space Complexity: O(n)
When to use?
Use when you need an efficient, low-level memory manipulationfor large data copies.
Conclusion
In this article, we explored four different methods to concatenate two strings in C: using the strcat() function, manually using loops, using sprintf(), and using memmove(). Each method has its unique benefits and can be selected depending on the needs of your program, such as whether you require simple concatenation, complex formatting, or efficient memory manipulation.
Frequently Asked Questions on C String Concatenation – FAQs
What is string concatenation?
String concatenation is the process of joining two strings end-to-end to create a single combined string.
Can I concatenate strings without using strcat()?
Yes, you can concatenate strings manually using loops, or by using functions like sprintf() and memmove().
Is there any risk in using strcat()?
Yes, if the destination string is not large enough to hold the combined string, it can lead to buffer overflow errors.
What is the difference between memmove() and strcat() for concatenation?
memmove() is a low-level function that allows efficient memory manipulation, making it useful for large data copies, while strcat() is simpler and more commonly used for basic string concatenation.
Please Login to comment...