How to Concatenate Multiple Strings in C++?
Last Updated :
01 Mar, 2024
In C++, to concatenate multiple strings means we need to combine two or more strings into a single string. In this article, we will learn how to concatenate multiple strings in C++.
Example
Input:
str1="Hello!"
str2="Geek,"
str3="Happy Coding."
Output:
Hello! Geek, Happy Coding.
Concatenate Multiple Strings in C++
To concatenate multiple strings into a single string in C++, we can use the (+) plus operator that appends the additional string(s) to the original string as a result original string is modified.
C++ Program to Concatenate Multiple Strings
The below example demonstrates the use of std::append() method to concatenate multiple string into one in C++.
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "Hello" ;
string str2 = " Geek!" ;
string str3 = " Happy Coding" ;
string concat_str = str1 + str2 + str3;
cout << "Concatenated String: " << concat_str << endl;
return 0;
}
|
Output
Concatenated String: Hello Geek! Happy Coding
Time Complexity: O(N), where N is the total number of characters in the all the strings.
Space Complexity: O(N)
Note: We can concatenate two C-Style strings using strcat() function.
Share your thoughts in the comments
Please Login to comment...