How to Handle Multiple String Inputs with Spaces in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, strings are a sequence of characters that might contain spaces in many cases but we can read only the input text till whitespace using cin. In this article, we will learn how to handle multiple string inputs with spaces in C++. Example: Input:Enter multiple strings:String1String2Output:You Entered: String1You Entered: String2Reading Multiple String Inputs with Spaces in C++To handle multiple string inputs with spaces in C++, we can use the std::getline() method provided by the STL library that is commonly used for reading a line from the input stream. It reads a string until the newline character occurs. C++ Program to Handle Multiple String Inputs with SpacesThe following program illustrates how we can handle multiple string inputs with spaces using the std::getline method in C++. C++ // C++ Program to Handle Multiple String Inputs with Spaces #include <iostream> #include <string> using namespace std; int main() { // Initialize a string to take input from the user string input; cout << "Enter multiple strings:" << endl; while (getline(cin, input)) { // Print multiple string inputs after each space cout << "You entered: " << input << endl; } return 0; } OutputEnter multiple strings: Output Enter multiple strings:String1You entered: String1String2You entered: String2Time Complexity: O(N*K) where N is the number of strings entered and K is average length of each string.Auxiliary Space: O(N*K) Comment More infoAdvertise with us Next Article How to Handle Multiple String Inputs with Spaces in C++? B bytebarde55 Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads How to Take Long String Input With Spaces in C++? In C++, we often take long strings with a lot of characters as inputs with spaces but if we use cin then our input gets terminated as soon as whitespace is encountered. In this article, we will discuss how to take long strings with spaces as input in C++. Take Long String Input With Spaces in C++To 2 min read How to Use stringstream for Input With Spaces in C++? In C++, the std::stringstream class is a stream class to operate on strings and is very useful when we want to operate on a string as if it were a stream (like cin or cout). In this article, we will learn how to use string streams for input with spaces in C++.Example:Input: string = âHello, World!âO 2 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read How to Take Multiple String Inputs in a Single Line in C++? In C++, strings are used to store textual data. While working with user input, we often may need to take multiple string inputs in a single line only. In this article, we will learn how to take multiple string inputs in a single line. Example Input: Hi,Geek,Welcome,to,GfG delimiter = ',' Output: Str 2 min read How to Take std::cin Input with Spaces? In C++, std::cin is used to accept the input from the standard input device. By default, the delimiter used by std::cin to separate input is a whitespace. In this article, we will learn how to take std::cin input with spaces in C++. Example: Input: Hey! Geek Welcome to GfG //input with spacesOutput: 2 min read Like