Concatenating Two Strings in C
Last Updated :
28 Mar, 2023
Given two strings str1 and str2, our task is to concatenate these two strings. There are multiple ways to concatenate two strings in C language:
- Without Using strcat() function
- Using standard method
- Using function
- Using recursion
- Using strcat() function
1. Concatenating Two strings without using the strcat() function
A. Using Standard Method
Input: str1 = "hello", str2 = "world"
Output: helloworld
Input: str1 = "Geeks", str2 = "World"
Output: GeeksWorld
Approach: Using ‘+’ operator
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Geeks" ;
string str2 = "ForGeeks" ;
string result = str1 + str2;
cout << result << endl;
return 0;
}
|
Approach: Using append function.
C++
#include <iostream>
using namespace std;
int main() {
string str1 = "hello" ;
string str2 = "world" ;
cout<< "The Resultant String Is :" <<endl;
cout<<str1.append(str2)<<endl;
return 0;
}
|
Output
The Resultant String Is :
helloworld
Complexity Analysis:
Time Complexity:O(1).
Auxiliary Space: O(1).
Approach:
- Get the two Strings to be concatenated
- Declare new Strings to store the concatenated String
- Insert the first string into the new string
- Insert the second string in the new string
- Print the concatenated string
Below is the implementation of the above approach:
C
#include <stdio.h>
int main()
{
char str1[100] = "Geeks" , str2[100] = "World" ;
char str3[100];
int i = 0, j = 0;
printf ( "\nFirst string: %s" , str1);
printf ( "\nSecond string: %s" , str2);
while (str1[i] != '\0' ) {
str3[j] = str1[i];
i++;
j++;
}
i = 0;
while (str2[i] != '\0' ) {
str3[j] = str2[i];
i++;
j++;
}
str3[j] = '\0' ;
printf ( "\nConcatenated string: %s" , str3);
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
char str1[100] = "Geeks" , str2[100] = "World" ;
char str3[100];
int i = 0, j = 0;
cout << "\nFirst string: " << str1;
cout << "\nSecond string: " << str2;
while (str1[i] != '\0' ) {
str3[j] = str1[i];
i++;
j++;
}
i = 0;
while (str2[i] != '\0' ) {
str3[j] = str2[i];
i++;
j++;
}
str3[j] = '\0' ;
cout << "\nConcatenated string: " << str3;
return 0;
}
|
Output
First string: Geeks
Second string: World
Concatenated string: GeeksWorld
Time complexity: O(m+n)
Auxiliary space : O(1)
B. Using Function
Approach:
- main function will call concatenate_string() function to concatenate two strings.
- The function will get the length of string s with the help of strlen.
- Now we will append the character of string s1 at s[i+j]. This step will be repeated till no character is available in s1. We are appending characters of string s1 to s from the end of s.
- After for loop, we will be concatenating the string s.
- At last main function will print the string which is concatenated.
C
#include <stdio.h>
#include <string.h>
void concatenate_string( char * s, char * s1)
{
int i;
int j = strlen (s);
for (i = 0; s1[i] != '\0' ; i++) {
s[i + j] = s1[i];
}
s[i + j] = '\0' ;
return ;
}
int main()
{
char s[5000], s1[5000];
printf ( "Enter the first string: " );
gets (s);
printf ( "Enter the second string: " );
gets (s1);
concatenate_string(s, s1);
printf ( "Concatenated String is: '%s'\n" , s);
return 0;
}
|
Output:
Enter the first string: Geeks
Enter the second string: forGeeks
Concatenated String is: 'GeeksforGeeks'
Time Complexity: O(n+m) , where n is size of string 1 and m is size of string 2 respectively.
Auxiliary Space: O(1)
C. Using Recursion
Approach:
- The function concatenate_string() will get the strings s and s1.
- if no elements are present in s1 then assign s1 with a null (\0) character.
- else if elements are present then we will add the element of string s1 at the end of the string s and will increase the value of i by 1.
- The function concatenate_string will call itself by passing the modified strings s, s1 as arguments. This function will call itself recursively until no elements are available in s1.
C
#include <stdio.h>
#include <string.h>
void concatenate_string( char * s, char * s1)
{
static int i = 0;
static int j = strlen (s);
if (!s1[i]) {
s1[i] = '\0' ;
}
else {
s[i + j] = s1[i];
i++;
concatenate_string(s, s1);
}
}
int main()
{
char s[5] = "Geeks" , s1[8] = "forGeeks;
concatenate_string(s, s1);
printf ( "\nConcatenated String is: '%s'\n" , s);
return 0;
}
|
Output:
Enter the first string: Geeks
Enter the second string: forGeeks
Concatenated String is: 'GeeksforGeeks'
Time Complexity: O(n+m) , where n is size of string 1 and m is size of string 2 respectively.
Auxiliary Space: O(1)
2. Using strcat() function
strcat() function in C appends the copy of the source string to the destination with a Null character at the end of the string. It comes under string.h header file in C.
C
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "Geeks" ;
char s1[] = "forGeeks" ;
strcat (s, s1);
printf ( "Final string is: %s " , s);
return 0;
}
|
C++
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char s[] = "Geeks" ;
char s1[] = "forGeeks" ;
strcat (s, s1);
cout << "Final string is: " << s;
return 0;
}
|
Output
Final string is: GeeksforGeeks
Time Complexity: O(n+m) , where n is size of string 1 and m is size of string 2 respectively.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...