String Concatenation in C++

What is String concatenation?

String concatenation in C++ is the process of combining multiple strings into a single string. It involves merging the characters or contents of two or more strings to create a new concatenated string.

For Example, first string = Hello; second string = World

Hello + World = Hello World
Problem Description

Write a C++ program that takes two strings as input and concatenates them.

Problem Solution

1. The program takes two strings initially.
2. Using string function, the two strings are concatenated.
3. The concatenated string is printed.
4. Exit.

String Concatenation Techniques/Methods

Let’s discuss various methods to concatenate strings in C++.

advertisement
advertisement
Method 1: Using strcat( ) Function

In this approach, we utilize the strcat() function in C++ to combine two C-style strings. It is a library function for string concatenation.

Program/Source code

Here is the source code of C++ Program to Concatenate Two Strings. The program output is shown below.

/*
 * C++ Program to Concatenate Two Strings using strcat( )
 */
 
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
    char str1[50], str2[50];
    cout << "Enter string 1 : ";
    gets(str1);
    cout << "Enter string 2 : ";
    gets(str2);
    strcat(str1, str2);
    cout << "Concatenated String : " << str1;
    return 0;
}
Program Explanation

1. The user is asked to enter two strings. They are stored in the character variables ‘str1‘ and str2‘.
2. Using ‘strcat()‘, which is an inbuilt function under the library ‘string.h’, the two strings are concatenated.
3. The concatenated string is stored in str1.
4. The result is then printed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Time Complexity: O(m + n)
The time complexity is O(m + n), where ‘m’ is the length of str1 and ‘n’ is the length of str2 due to the strcat() function.

Space Complexity: O(1)
The space complexity is O(1), as the memory used remains constant, regardless of the input size.

Runtime Test Cases

Testcase 1: In this case, we are entering the strings to concatenate are “Hello” and “World” as input.

Enter string 1 : Hello
Enter string 2 : World
Concatenated String : Hello World

Testcase 2: In this case, we are entering the strings “C++” and “Programming” as input.

advertisement
Enter string 1 : C++
Enter string 2 : Programming
Concatenated String : C++ Programming

Method 2: Using append() Function

In this approach, we use the append() function in C++ to combine two C-style strings. append() is a member function that concatenates strings or adds characters to the end of a string.

Program/Source code

Here is the source code of C++ Program to Concatenate Two Strings. The program output is shown below.

/*
 * C++ Program to Concatenate Two Strings using append()
 */
 
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    string str1, str2;
    cout << "Enter string 1: ";
    getline(cin, str1);
    cout << "Enter string 2: ";
    getline(cin, str2);
    str1.append(str2);
    cout << "Concatenated String: " << str1;
    return 0;
}
Program Explanation

1. The program prompts the user to input two strings. The entered strings are stored in the string variables str1 and str2.
2. The program uses the append() function from the string class, which is a member function available in the <string> library. This function concatenates the contents of str2 to the end of str1.
3. After using append(), the concatenated string is now stored in str1.
4. The program prints the concatenated result, which is the combined content of the two entered strings, using the cout statement.

advertisement

Time Complexity: O(m)
The append() function has a time complexity of O(m), where m is the length of the string being appended.

Space Complexity: O(n+m)
The space complexity is O(n+m), where n is the length of str1 and m is the length of str2 due to the creation of a new string.

Program Output

In this case, we are entering the strings to concatenate are “Sanfoundry” and “Programming” as input.

Enter string 1: Sanfoundry
Enter string 2: Programming
Concatenated String: Sanfoundry Programming

Method 3: Using ‘+’ Operator

In this approach, we use the ‘+’ operator to concatenate two C-style strings.

Program/Source code

Here is the source code of C++ Program to Concatenate Two Strings. The program output is shown below.

/*
 * C++ Program to Concatenate Two Strings using ‘+’ Operator
 */
 
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    string str1, str2;
    cout << "Enter string 1: ";
    getline(cin, str1);
    cout << "Enter string 2: ";
    getline(cin, str2);
    string concatenatedString = str1 + str2;
    cout << "Concatenated String : " << concatenatedString;
    return 0;
}
Program Explanation

1. The program prompts the user to enter two strings, which are stored in the string variables str1 and str2.
2. The program uses the + operator to concatenate the two strings. The + operator for string objects automatically concatenates their contents.
3. The concatenated string is now stored in str1.
4. Finally, the program prints the concatenated result, which is the combined content of the two entered strings, using the cout statement.

Time Complexity: O(n+m)
The time complexity of this program is O(n+m), where n and m are the lengths of the two input strings.

Space Complexity: O(n+m)
The space complexity is O(n+m), where n and m are the lengths of the two input strings, due to the creation of a new concatenatedString.

Program Output

In this case, we are entering the strings to concatenate are “Programming” and “is fun” as input.

Enter string 1: Programming
Enter string 2: is fun
Concatenated String: Programming is fun

Method 4: String Concatenation in C++ Using for Loop

In this approach, we use the for loop to concatenate two C-style strings.

Program/Source code

Here is the source code of C++ Program to Concatenate Two Strings. The program output is shown below.

/*
 * C++ Program to Concatenate Two Strings using for Loop
 */
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char str1[100], str2[50];    
    cout << "Enter string 1 : ";
    cin.getline(str1, sizeof(str1));
    cout << "Enter string 2 : ";
    cin.getline(str2, sizeof(str2));
    int len1 = strlen(str1);
    int len2 = strlen(str2);
 
    // Concatenate the string using a for loop
    for (int i = 0; i < len2; i++)
    {
        str1[len1 + i] = str2[i];
    }
    str1[len1 + len2] = '\0';
    cout << "Concatenated String : " << str1;
    return 0;
}
Program Explanation

1. The program prompts the user to enter two strings, which are stored in the character arrays str1 and str2.
2. The program utilizes a for loop to manually concatenate str2 to the end of str1. Each character of str2 is appended to the end of str1 until the loop reaches the null character ‘\0’ indicating the end of str2.
3.The concatenated string is now stored in str1.
4. Finally, the result is printed.

Time Complexity: O(m)
The time complexity is O(m), where m is the length of the second input string str2.

Space Complexity: O(n+m)
The space complexity is O(n+m), where n and m are the lengths of the two input strings due to the creation of a new concatenated string.

Program Output

In this case, we are entering the strings to concatenate are “Lets” and “code” as input.

Enter string 1: Lets
Enter string 2: code
Concatenated String: Lets code

To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.