stringstream in C++ and its applications - GeeksforGeeks

30 Jan, 2022
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. 
Basic methods are:
clear()- To clear the stream.str()- To get and set string object whose content is present in the stream. operator <<- Add a string to the stringstream object. operator >>- Read something from the stringstream object.
clear()- To clear the stream.
str()- To get and set string object whose content is present in the stream. 
operator <<- Add a string to the stringstream object. 
operator >>- Read something from the stringstream object.
Examples:
1. Count the number of words in a string-
Examples-
Input: Asipu Pawan KumarOutput: 3
Input: Geeks For Geeks IdeOutput: 4
Below is the C++ program to implement the above approach-
C++
// C++ program to count words in // a string using stringstream.#include <iostream>#include <sstream>#include<string>using namespace std; int countWords(string str){    // Breaking input into word    // using string stream       // Used for breaking words    stringstream s(str);       // To store individual words    string word;     int count = 0;    while (s >> word)        count++;    return count;} // Driver codeint 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-
Examples-
Input: Geeks For Geeks Quiz Geeks Quiz Practice PracticeOutput: For -> 1             Geeks -> 3             Practice -> 2             Quiz -> 2
Input: Word String Frequency StringOutput: Frequency -> 1              String -> 2              Word -> 1     
Below is the C++ program to implement the above approach-
C++
// C++ program to demonstrate use// of stringstream to count// frequencies of words.#include <bits/stdc++.h>#include <iostream>#include <sstream>#include<string>using namespace std; void printFrequency(string st){    // Each word it mapped to    // it's frequency    map<string, int>FW;       // Used for breaking words    stringstream ss(st);       // To store individual words    string Word;     while (ss >> Word)        FW[Word]++;     map<string, int>::iterator m;    for (m = FW.begin(); m != FW.end(); m++)        cout << m->first << "-> "             << m->second << "\n";} // Driver codeint main(){    string s = "Geeks For Geeks Ide";    printFrequency(s);    return 0;}
For-> 1
Geeks-> 2
Ide-> 1

3. Removing spaces from a string using Stringstream4. 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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
ghurarpit110
cpp-string
cpp-stringstream
C++
Strings
Strings
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Virtual Function in C++
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack