Concatenate Two Strings in C++

We can concatenate two strings in C++ language. Concatenation of two strings are nothing but adding two given strings as one string.

We can concatenate two strings using two methods such as using string objects and using c-style strings.

Concatenate Two Strings in C++

Concatenate Two Strings Using string objects

String objects are nothing but the objects which gets string as a input. We can get two string by using string objects and concatenate them using the formula result = s1 + s2;

#include <iostream>
using namespace std;

int main()
{
    string s1, s2, result;

    cout << "Enter string s1: ";
    getline (cin, s1);

    cout << "Enter string s2: ";
    getline (cin, s2);

    result = s1 + s2;

    cout << "Resultant String = "<< result;

    return 0;
}

Output

Enter string s1: C++ Programming
Enter string s2:  is awesome.
Resultant String = C++ Programming is awesome.

Concatenate Two Strings using c-style strings.

Strcat() is a default function which is used to concatenate two strings in C++ language. Here the two strings are concatenated using strcat() function.

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s1[50], s2[50];

    cout << "Enter string s1: ";
    cin.getline(s1, 50);

    cout << "Enter string s2: ";
    cin.getline(s2, 50);

    strcat(s1, s2); 

    cout << "s1 = " << s1 << endl;
    cout << "s2 = " << s2;

    return 0;
}
Read Also : Find the Roots of a Quadratic Equation in C++

Output

The output screen asks you to enter the string s1 and string s2 separately. Then it concatenates two strings using strcat() function and displays output.

Enter string s1: I love        
Enter string s2:  C++ programming 
s1 = I love C++ programming
s2 =  C++ programming

Final Words

I hope this article helps you to Concatenate Two Strings in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment