Open In App

Concatenating Two Strings in C

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


Output

GeeksForGeeks

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




// 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++




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

  • 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




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

  • 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




// 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




// 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;
}


C++




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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads