Open In App

Concatenating Two Strings in C

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:

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






#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    string str1 = "Geeks";
    string str2 = "ForGeeks";
    string result = str1 + str2;
    cout << result << endl;
    return 0;
}

Output
GeeksForGeeks

Approach: Using append function.






#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:

Below is the implementation of the above approach: 




// C Program to concatenate two
// strings without using strcat
#include <stdio.h>
   
int main()
{
   
    // Get the two Strings to be concatenated
    char str1[100] = "Geeks", str2[100] = "World";
   
    // Declare a new Strings
    // to store the concatenated String
    char str3[100];
   
    int i = 0, j = 0;
   
    printf("\nFirst string: %s", str1);
    printf("\nSecond string: %s", str2);
   
    // Insert the first string
    // in the new string
    while (str1[i] != '\0') {
        str3[j] = str1[i];
        i++;
        j++;
    }
   
    // Insert the second string
    // in the new string
    i = 0;
    while (str2[i] != '\0') {
        str3[j] = str2[i];
        i++;
        j++;
    }
    str3[j] = '\0';
   
    // Print the concatenated string
    printf("\nConcatenated string: %s", str3);
   
    return 0;
}




// C++ Program to concatenate two
// strings without using strcat
#include <iostream>
using namespace std;
   
int main()
{
   
    // Get the two Strings to be concatenated
    char str1[100] = "Geeks", str2[100] = "World";
   
    // Declare a new Strings
    // to store the concatenated String
    char str3[100];
   
    int i = 0, j = 0;
   
    cout <<"\nFirst string: "<< str1;
    cout <<"\nSecond string: "<< str2;
   
    // Insert the first string
    // in the new string
    while (str1[i] != '\0') {
        str3[j] = str1[i];
        i++;
        j++;
    }
   
    // Insert the second string
    // in the new string
    i = 0;
    while (str2[i] != '\0') {
        str3[j] = str2[i];
        i++;
        j++;
    }
    str3[j] = '\0';
   
    // Print the concatenated string
    cout <<"\nConcatenated string: "<< str3;
   
    return 0;
}
 
// this code is contributed by shivanisingh

Output
First string: Geeks
Second string: World
Concatenated string: GeeksWorld

Time complexity: O(m+n)
Auxiliary space : O(1)

B. Using Function

Approach:




// C program to concatenating two
// strings using function
#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);
 
    // function concatenate_string
    // called and s and s1 are
    // passed
    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:




// C program to concatenate two
// strings with the help of
// recursion
#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;
 
    // function concatenate_string
    // called and s1 and s2 are
    // passed
    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 program to concatenate two
// strings using strcat function
#include <stdio.h>
#include <string.h>
 
int main()
{
 
    char s[] = "Geeks";
    char s1[] = "forGeeks";
 
    // concatenating the string
    strcat(s, s1);
    printf("Final string is: %s ", s);
    return 0;
}




#include <cstring>
#include <iostream>
using namespace std;
 
int main()
{
 
    char s[] = "Geeks";
    char s1[] = "forGeeks";
 
    // concatenating the string
    strcat(s, s1);
    cout << "Final string is: " << s;
    return 0;
}
// This code is contributed by Akshay
// Tripathi(akshaytripathi630)

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)


Article Tags :