How to Use stringstream for Input With Spaces in C++? Last Updated : 06 Jul, 2024 Comments Improve Suggest changes 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 Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 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 How to Read Input Until EOF in C++? In C++, EOF stands for End Of File, and reading till EOF (end of file) means reading input until it reaches the end i.e. end of file. In this article, we will discuss how to read the input till the EOF in C++. Read File Till EOF in C++The getline() function can be used to read a line in C++. We can 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 Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read Like