How to Use stringstream for Input With Spaces in C++? Last Updated : 06 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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!”Output: Hello, World! //Read the string into a string stream and output each wordTaking Input with Spaces Using stringstream in C++In C++, the stringstream is used to create a stream to a string. This stream can be used to read the data from the string using >> (extraction operator) but it will read the input till the whitespace. The rest of the input would require more use of the>> operator making it not suitable for working with input strings with spaces.To use stringstream for strings with spaces, we can use the std::getline() function that can read from the stream till the given delimiter.Syntax of std::getline() in C++getline(stream, dest, delim)Here,stream: stream to read data fromdest: string variable that will hold the read data.delim: delimiter that specifies where to stop reading.C++ Program to Use String Streams for Input with Spaces The below example demonstrates how to use string streams for input with spaces in C++. C++ // C++ Program to illustrate how to use stringstream for input with spaces #include <iostream> #include <sstream> using namespace std; int main() { // Initialize a string string myString = "Hello, World!"; // Use stringstream for input with spaces stringstream ss(myString); string word; while (getline(ss, word, '\0')) { cout << word << endl; } return 0; } OutputHello, World! Time Complexity: O(N), here N is the length of the stringAuxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Use stringstream for Input With Spaces in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-stringstream CPP Examples +1 More Practice Tags : CPP 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 Handle Multiple String Inputs with Spaces in C++? 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 E 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 How to input a comma separated string in C++? Given an input string which is comma-separated instead of space, the task is to parse this input string in C++.First, let us understand what difference does it create if the input string is comma-separated. Taking input a whitespace-separated stringTaking input a whitespace-separated string in C++ i 2 min read How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to 2 min read Like