How to Concatenate Multiple C++ Strings on One Line?
Last Updated :
09 Feb, 2024
In C++, strings are used to store a sequence of characters. The combination of multiple strings into a single one is called Concatenation. In this article, we will learn how to concatenate multiple strings in C++.
Example
Input:
str1="Hello!"
str2="Geek"
str3="Welcome to GfG"
Output:
Hello! Geek Welcome to GfG
Concatenate Multiple C++ Strings in One Line
In the std::string class, the ‘+’ operator is overloaded to concatenate two strings. We can use this ‘+’ operator of the std::string to concatenate multiple strings into a single line.
C++ Program to Concatenate Multiple Strings in One Line
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Hello!" ;
string s2 = " Geek" ;
string s3 = " Welcome to GfG" ;
string ans = s1 + s2 + s3;
cout << "The Concatenated string is : " << ans << endl;
return 0;
}
|
Output
The Concatenated string is : Hello! Geek Welcome to GfG
Time Complexity: O(N), where N is the total length of all the strings being concatenated.
Auxiliary Space: O(N)
We can also use strcat() and append() function to concatenate multiple strings.
Share your thoughts in the comments
Please Login to comment...