In this C++ tutorial, you will learn how to concatenate two or more strings using String Concatenation Operator, with examples.
Concatenate Strings
To concatenate two or more strings in C++, use string concatenation operator +
.
Syntax
The syntax to concatenate two strings str1
and str2
is
str1 + str2
The above expression returns the resulting string of the two strings concatenated in the given order.
We may concatenate more than two strings in a single statement.
str1 + str2 + str3
Examples
1. Concatenate two strings
In the following program, we take two strings: str1
and str2
and concatenate them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "apple";
string str2 = " banana";
string result = str1 + str2;
cout << result << endl;
}
Output
apple banana
Program ended with exit code: 0
2. Concatenate multiple strings
In the following program, we take three strings: str1
, str2
and str3
and concatenate them in the same order and store the result in variable result
.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "apple";
string str2 = " banana";
string str3 = " cherry";
string result = str1 + str2 + str3;
cout << result << endl;
}
Output
apple banana cherry
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned how to concatenate two or more strings in C++, with examples.