Table of contents
1.
How to Create a StringStream in C++?
1.1.
Example of std::stringstream
1.2.
C++
2.
Examples of  StringStream in C++
2.1.
1. Count the number of words in a string
2.1.1.
Approach
2.1.2.
Implementation in C++
2.2.
C++
2.3.
2. Divide string of words into different strings
2.3.1.
Approach
2.3.2.
Implementation in C++
2.4.
C++
2.5.
3. Remove spaces from the string
2.5.1.
Approach
2.5.2.
Implementation in C++
2.6.
C++
2.6.1.
Check out this article - C++ String Concatenation
3.
Frequently Asked Questions
3.1.
How to create a stringstream from input string ‘str’? 
3.2.
How to clear the completely content present in stringstream ss? 
3.3.
What are classes in c++?
3.4.
Why use stringstream instead of string?
3.5.
What is the difference between Stringstream and Ostream in C++?
4.
Conclusion
Last Updated: Oct 7, 2024
Easy

StringStream in C++

Author Vaibhav Agarwal
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
StringStream in C++ and its Applications

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 

  1. clear() - It is used to clear the stream 
  2. str() - To get and set the string object that contains the stream's content.
  3. Operator <<: A string will be added to the stringstream object.
  4. 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:

  • C++

C++

#include <iostream>
#include <sstream> // Include the sstream header

int main() {
// Creating a string stream object
std::stringstream ss;

// Adding data to the string stream
ss << "Hello, " << "Ninjas! " << 2024;

// Extracting the string from the stream
std::string result = ss.str();

// Printing the result
std::cout << "StringStream contains: " << result << std::endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

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++

  • C++

C++

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
int countWords(string input)
{
   // Breaking input string into words
   // using stringStream 
   stringstream str(input);

   // To store individual words
   string word;
 
   // count variable to store the final count
   // of the number of words in the input string
   int count = 0;
   while (str >> word)
       count++;
   return count;
}

// Main Code
int main()
{
   string input = "Coding Ninja is an EdTech company";
   cout << " Number of words in the input string: " << countWords(input);
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

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++

  • C++

C++

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
void printString(string input)
{
   // Breaking input string into words
   // using stringStream 
   stringstream str(input);

   // To read individual words
   string word;
 
   // output vector used to store
   // each word from the input string
   vector<string>output;
   while (str >> word){
       output.push_back(word);
   }

   // Printing the content of vector
   for(auto it:output){
       cout << it << endl;
   }
}

// Main Code
int main()
{
   string input = "Coding Ninja is an EdTech company";
   printString(input);
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

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++

  • C++

C++

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
string removeSpaces(string input)
{
   // Breaking input string into words
   // using stringStream 
   stringstream str(input);

   // To read individual words
   string word;
 
  // temporary string to store
  // the string without spaces
  string temp = "";
   while (str >> word){
       temp += word;
   }

   input = temp;
   return input;
}

// Main Code
int main()
{
   string input = "Coding Ninja is an EdTech company";
   cout << removeSpaces(input) << endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

CodingNinjasisanEdtechcompany. 

Check out this article - C++ String Concatenation

Frequently Asked Questions

How to create a stringstream from input string ‘str’? 

The syntax for creating stringstream from the input string str is: stringstream ss(str). 

How to clear the completely content present in stringstream ss? 

The syntax for clear the same is: ss.clear(). 

What are classes in c++?

The class in C++ is the foundation for Object-Oriented programming. It's a user-defined data type with its own set of data members and member methods that can be accessed and used by establishing a class instance. A C++ class is similar to an object's blueprint.

Why use stringstream instead of string?

stringstream allows you to easily format, concatenate, and parse data in a stream-like manner, making it more versatile for handling complex string operations compared to a plain string.

What is the difference between Stringstream and Ostream in C++?

stringstream is used for in-memory string operations like reading and writing, while ostream handles output to external sources like console or files. stringstream operates on strings, whereas ostream outputs to devices.

Conclusion

In this article, we discussed the StringStream in C++ and its Applications. StringStream in C++ is a powerful utility for dynamically and flexibly handling the strings. If we want to format, concatenate, or parse data, stringstream is there to help us.

Recommended Readings:


If you are a beginner, interested in coding, and want to learn DSA, you can look for our guided path for DSA, which is free! 

Live masterclass