The Wayback Machine - https://web.archive.org/web/20240717024348/https://www.geeksforgeeks.org/concatenating-two-strings-in-c/
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)



Previous Article
Next Article

Similar Reads

C program to Compare Two Strings without using strcmp() function
Given two strings s1 and s2, the task is to write C program compare the two strings without using strcmp() function. If string are equal then print "Equal strings" else print "Unequal strings". Examples: Input: s1 = "geeksforgeeks", s2 = "geeks"Output: Unequal Strings Input: s1 = "geeksforgeeks", s2 = "geeksforgeeks"Output: Equal Strings Approach:
2 min read
Check if two strings are same or not without using library functions
Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”Output: TrueExplanation:S1 and S2 are the same strings Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”Output: False Approach: Follow the steps below to solve the prob
5 min read
C Program to Compare Two Strings Lexicographically
Here, we will build a C Program to compare two strings lexicographically. Given 2 strings s1 and s2, of lowercase English alphabets, denote the lexicographically greater string. Lexicographical order is the order in which words appear in the order of a dictionary. Input: s1 = "geeks", s2 = "geeksforgeeks Output: String 2 is lexicographically greate
2 min read
Program to check if two strings are same or not
Given two strings, the task is to check if these two strings are identical(same) or not. Examples: Input: string1 = "GeeksforGeeks", string2 = "GeeksforGeeks" Output: Yes Input: string1 = "Geeks for Geeks", string2 = "Geeks for Geeks" Output: Yes Input: string1 = "GeeksforGeeks", string2 = "Geeks" Output: No Input: string1 = "Geeks for Geeks", stri
7 min read
C Program to Concatenate Two Strings Without Using strcat
In C, concatenating two strings means combining two strings to form one string. In this article, we will learn how to concatenate two strings without using the strcat() function in C. For Example, Input: str1= "Hello,"str2= "Geek!"Output:Concatenated String: Hello, Geek!Concatenating Two Strings in CTo concatenate two strings without using the strc
2 min read
C Program to Concatenate Two Strings Using a Pointer
In C, concatenation of strings is nothing but it is the process of combining two strings to form a single string. If there are two strings, then the second string is added at the end of the first string. In this article, we are going to learn how to concatenate two strings using a pointer in C. Example: Input: str1 = "Hello"; str2 = " GeeksforGeeks
2 min read
C Program to Compare Two Strings Using Pointers
In C, strings are arrays of characters terminated by a null character ('\0') and pointers can be used to traverse these arrays and compare the characters in two strings. In this article, we will learn how to compare two strings using pointers in C. For Example, Input:char str1[] = "Hello";char str2[] = "World";Output:Hello and World are not EqualCo
2 min read
C Program to Split a String into a Number of Sub-Strings
Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter.  In C:  // Splits str[] according to given delimiters. // and returns next token. It needs to be
1 min read
C Program to check if strings are rotations of each other or not
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = str1.str1 2. If str2 is a substring of temp then
3 min read
C/C++ Program to Count number of binary strings without consecutive 1's
Write a C/C++ program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3// The 3 strings are 00, 01, 10 Input: N = 3Output: 5// The 5 strings are 000, 001, 010, 100, 101 Recommended: Please solve it on “PRACTICE ” first, before
4 min read
three90RightbarBannerImg