Open In App

How to Concatenate Multiple C++ Strings on One Line?

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to concatenate mutiple strings
#include <iostream>
#include <string>
using namespace std;
  
int main()
{
    // Initialize sttings
    string s1 = "Hello!";
    string s2 = " Geek";
    string s3 = " Welcome to GfG";
    
    // concatenate string using + operator
    string ans = s1 + s2 + s3;
  
    // printing concatenated string
    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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads