Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

stringstream in C++ and its applications

  • Difficulty Level : Medium
  • Last Updated : 09 May, 2018

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin).

Basic methods are –

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

clear() — to clear the stream
str() — to get and set string object whose content is present in stream.
operator << — add a string to the stringstream object.
operator >> — read something from the stringstream object,

stringstream class is extremely useful in parsing input.

Applications :

  1. Count number of words in a string
    Input : Asipu Pawan Kumar
    Output : 3
    Input : Geeks For Geeks Ide
    Output : 4

    // CPP program to count words in a string
    // using stringstream.
    #include <bits/stdc++.h>
    using namespace std;
    int countWords(string str)
        // breaking input into word using string stream
        stringstream s(str); // Used for breaking words
        string word; // to store individual words
        int count = 0;
        while (s >> word)
        return count;
    // Driver code
    int main()
        string s = "geeks for geeks geeks "
                   "contribution placements";
        cout << " Number of words are: " << countWords(s);
        return 0;


      Number of words are: 6
  2. Print frequencies of individual words in a string
    Input : Geeks For Geeks Quiz Geeks Quiz Practice Practice
    Output : For -> 1
             Geeks -> 3
             Practice -> 2
             Quiz -> 2
    Input : Word String Frequency String
    Output : Frequency -> 1
             String -> 2
             Word -> 1 

    // CPP program to demonstrate use of stringstream
    // to count frequencies of words.
    #include <bits/stdc++.h>
    using namespace std;
    void printFrequency(string st)
        // each word it mapped to it's frequency
        map<string, int> FW;
        stringstream ss(st); // Used for breaking words
        string Word; // To store individual words
        while (ss >> Word)
        map<string, int>::iterator m;
        for (m = FW.begin(); m != FW.end(); m++)
            cout << m->first << " -> "
                 << m->second << "\n";
    // Driver code
    int main()
        string s = "Geeks For Geeks Ide";
        return 0;


    For -> 1
    Geeks -> 2
    Ide -> 1
  3. Removing spaces from a string using Stringstream
  4. Converting Strings to Numbers in C/C++

This article is contributed by ASIPU PAWAN KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!