
A stringstream class in C++ is a Stream Class that works with strings. The stringstream class implements Memory-based stream input/output operations, i.e., string:
The stringstream class in C++ may be used to manage a string object as a stream. It's a string manipulation tool. We can extract and insert data from and into strings by treating them as streams, much like cin and cout streams.
These operations are frequently used to convert textual data types to numerical data types and the other way around. The stringstream class is also useful for a variety of parsing tasks.
In the following diagram, we can see where the stringstream class enters into play. The iostream class is a descendant of this one. Objects of the stringstream class use a string buffer storing a sequence of characters. As a string object, this buffer may be accessed directly.
For this, we may use the str component of the stringstream. In order to use the stringstream class in a C++ application, we must need the <sstream> header.
The Stringstream has different methods. Some of them are
- clear() - It is used to clear the stream
- str() - To get and set the string object that contains the stream's content.
- Operator <<: A string will be added to the stringstream object.
-
Operator >>: This method is used To read from a stringstream object.
Also see, Literals in C.
How to Create a StringStream in C++?
In C++, std::stringstream is a part of the <sstream> header, and it allows you to create a stream-like object that operates on strings. This is particularly useful for parsing and formatting data, similar to using input and output streams with files or console I/O.
Example of std::stringstream
Here’s how you can create and use a std::stringstream:
Output:
StringStream contains: Hello, Ninjas! 2024
Examples of StringStream in C++
1. Count the number of words in a string
Let us see some examples to better understand stringstream. Count the number of words in a string using stringstream.
Example 1:
Input: Coding Ninja is an EdTech company.
Output: 6
Explanation: There are 6 words in the string.
Example 2:
Input: Learn coding at Coding Ninjas Studio.
Output: 4
Explanation: There are 4 words in the string.
Approach
We will use stringstream to initially make the input string read word by word. Then we will use the count variable to count the number of words and finally print the final count.
Implementation in C++
Output:
Number of words in the input string 6.
2. Divide string of words into different strings
We will use stringstream to divide the string of words into different strings.
Example 1:
Input: Coding Ninja is an EdTech company
Output:
Coding
Ninja
is
an
EdTech
company
Explanation: There are 6 words in the string and we divide all the words into the 6 different strings and printed them.
Example 2:
Input: Learn coding at Coding Ninjas Studio.
Output:
Learn
coding
at
Coding Ninjas Studio
Explanation: There are 4 words in the string and we divide all the words into 4 different strings and printed them.
Approach
We will use stringstream to initially make the input string read word by word, then we will push each word into vector and then print each string from the vector.
Implementation in C++
Output::
Coding
Ninja
is
an
EdTech
company
You practice by yourself with the help of online c++ compiler.
3. Remove spaces from the string
We will use stringstream to remove spaces from the string.
Example 1:
Input: Coding Ninjas is an EdTech Company.
Output: CodingNinjasisanEdTechCompnay.
Explanation: We have removed all the spaces between the words of the string.
Example 2:
Input: Learn coding at Coding Ninjas Studio.
Output: LearnCodingatCoding Ninjas Studio.
Explanation: We have removed all the spaces between the words of the string.
Approach
We will use stringstream to initially make the input string read word by word, Then we will read the string word by word and add a word into the temporary string so that spaces between the words are removed, and finally make the original string equal to the temporary string.
Implementation in C++
Output:
CodingNinjasisanEdtechcompany.