How to Handle Multiple String Inputs with Spaces in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes 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) Create Quiz Comment B bytebarde55 Follow 1 Improve B bytebarde55 Follow 1 Improve Article Tags : C++ Programs C++ cpp-input-output cpp-strings CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like