Given a string containing space-separated words and a number K. The task is to find and print all those words whose length is greater than K using stringstream in C++.
A general solution to solve this problem using loops is discussed in the previous article. In this article, a solution using stringstream in C++ will be discussed.
Examples:
Input : str = "hello geeks for geeks
is computer science portal"
K = 4
Output : hello geeks geeks computer
science portal
Input : str = "string is fun in python"
K = 3
Output : string python
The idea is to use stringstream to create a stream by splitting the given string into tokens and then process the stream and print the words with length greater than K.
Below is the implementation of the above idea:
// C++ program to find all string // which are greater than given length k // using stringstream #include <bits/stdc++.h> using namespace std; // Function to find all string // which are greater than given length k // using stringstream void findWords(string str, int K) { string word; // using stringstream to break // the string into tokens stringstream ss(str); int count = 0; while (ss >> word) { // reading words if (word.size() > K) { cout << word << " "; count++; } } } // Driver code int main() { string str = "geeks for geeks"; int k = 4; findWords(str, k); return 0; } |
geeks geeks
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.
Recommended Posts:
- Find words which are greater than given length k
- Count the number of words having sum of ASCII values less than and greater than k
- Generating Lyndon words of length n
- K length words that can be formed from given characters without repetition
- Java program to print Even length words in a String
- stringstream in C++ and its applications
- StringStream in C++ for Decimal to Hexadecimal and back
- Removing spaces from a string using Stringstream
- Smallest Greater (than S) String of length K whose letters are subset of S
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Check if the given string of words can be formed from words present in the dictionary
- Python program to find uncommon words from two Strings
- Find alphabetical order such that words can be considered sorted
- Find the size of largest subset of anagram words
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find and print duplicate words in std::vector<string> using STL functions
- Count words that appear exactly two times in an array of words
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Find max length odd parity substring
- 5 Different methods to find length of a string in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

