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 - Read something from the stringstream object.
Examples:
1. Count the number of words in a string
Examples:
Input: Asipu Pawan Kumar
Output: 3Input: Geeks For Geeks Ide
Output: 4
Below is the C++ program to implement the above approach-
C++
// C++ program to count words in
// a string using stringstream.
#include
#include
#include
using
namespace
std;
For-> 1 Geeks-> 2 Ide-> 10
For-> 1 Geeks-> 2 Ide-> 11
For-> 1 Geeks-> 2 Ide-> 12
For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 14
For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 16
For-> 1 Geeks-> 2 Ide-> 17
For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 19
For-> 1 Geeks-> 2 Ide-> 13
// C++ program to count words in
1For-> 1 Geeks-> 2 Ide-> 17
For-> 1 Geeks-> 2 Ide-> 13
// C++ program to count words in
4For-> 1 Geeks-> 2 Ide-> 13
// C++ program to count words in
6
For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 10
// C++ program to count words in
9For-> 1 Geeks-> 2 Ide-> 13
// a string using stringstream.
1 // a string using stringstream.
2// a string using stringstream.
3// a string using stringstream.
4
For-> 1 Geeks-> 2 Ide-> 13
// a string using stringstream.
6 // a string using stringstream.
7// a string using stringstream.
8
// a string using stringstream.
9
For-> 1 Geeks-> 2 Ide-> 10
#include
1For-> 1 Geeks-> 2 Ide-> 12
For-> 1 Geeks-> 2 Ide-> 13
#include
4#include
5#include
6#include
7#include
8
For-> 1 Geeks-> 2 Ide-> 13
#include
0#include
1 #include
2For-> 1 Geeks-> 2 Ide-> 13
// a string using stringstream.
6 #include
5// a string using stringstream.
8
Output
Number of words are: 6
2. Print frequencies of individual words in a string
Examples:
Input: Geeks For Geeks Quiz Geeks Quiz Practice Practice
Output: For -> 1
Geeks -> 3
Practice -> 2
Quiz -> 2Input: Word String Frequency String
Output: Frequency -> 1
String -> 2
Word -> 1
Below is the C++ program to implement the above approach-
C++
#include
7
#include
8
#include
9
#include
0
#include
#include
#include
using
namespace
std;
#include
7 #include
8
For-> 1 Geeks-> 2 Ide-> 12
For-> 1 Geeks-> 2 Ide-> 13
using
1For-> 1 Geeks-> 2 Ide-> 13
using
3For-> 1 Geeks-> 2 Ide-> 13
using
5For-> 1 Geeks-> 2 Ide-> 10
using
7For-> 1 Geeks-> 2 Ide-> 17
For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 19
For-> 1 Geeks-> 2 Ide-> 13
namespace
2For-> 1 Geeks-> 2 Ide-> 17
For-> 1 Geeks-> 2 Ide-> 13
// C++ program to count words in
4For-> 1 Geeks-> 2 Ide-> 13
namespace
7
For-> 1 Geeks-> 2 Ide-> 13
// a string using stringstream.
1 std;
0// a string using stringstream.
3std;
2
For-> 1 Geeks-> 2 Ide-> 13
using
5For-> 1 Geeks-> 2 Ide-> 10
std;
6For-> 1 Geeks-> 2 Ide-> 13
std;
8 std;
9// a string using stringstream.
3
For-> 1 Geeks-> 2 Ide-> 101
For-> 1 Geeks-> 2 Ide-> 102
For-> 1 Geeks-> 2 Ide-> 103
For-> 1 Geeks-> 2 Ide-> 104
For-> 1 Geeks-> 2 Ide-> 105
#include
8// a string using stringstream.
8
// a string using stringstream.
9
For-> 1 Geeks-> 2 Ide-> 10
#include
1For-> 1 Geeks-> 2 Ide-> 12
For-> 1 Geeks-> 2 Ide-> 13
#include
4For-> 1 Geeks-> 2 Ide-> 114
#include
8For-> 1 Geeks-> 2 Ide-> 13
For-> 1 Geeks-> 2 Ide-> 117
For-> 1 Geeks-> 2 Ide-> 13
// a string using stringstream.
6 #include
5// a string using stringstream.
8
Output
For-> 1 Geeks-> 2 Ide-> 1
Removing spaces from a string using Stringstream
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.