Concatenating two string using + (plus) operator in C++ STL

C++ STL | concatenating two strings: In this article, we are going to see how we can concatenate two strings using (plus) '+' like other default datatypes?
Submitted by Radib Kar, on February 27, 2019

String as datatype

In C, we know string basically a character array terminated by \0. Thus to operate with the string we define character array. But in C++, standard library gives us the facility to use string as a basic data type like integer.


    Like we define and declare,
    int i=5,j=7,k;
    Same way, we can do for a string like,
    string s1="Include", s2="Help";

    k=i+j; // 12
    Same way we can concatenate our two string s1 & s2
    string s3=s1+s2;
    now s3 is "IcludeHelp" 
    string s4=s2+s1;
    s4 is "HelpInclude"

Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.

Header file needed:

    #include <string>
    #include <bits/stdc++.h>

C++ program to concatenate strings using plus (+) operator

#include <bits/stdc++.h>
using namespace std;

int main(){
	string s1,s2,s3,s4;

	cout<<"Enter your string1\n";
	cout<<"Enter your string2\n";

	cout<<"Stings concatenated...\n";
	cout<<"Concat(s1,s2): "<<s3<<endl;
	cout<<"Concat(s2,s1): "<<s4<<endl;

	return 0;


Enter your string1
Enter your string2
Stings concatenated...    
Concat(s1,s2): IncludeHelp
Concat(s2,s1): HelpInclude

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.