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 neveropen for neveropen is computer science portal" K = 4 Output : hello neveropen neveropen 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:
Implementation:
C++
// 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 = "neveropen for neveropen" ; int k = 4; findWords(str, k); return 0; } |
Java
// Java program to find all string // which are greater than given length k // using StringTokenizer import java.util.*; class Main { // Function to find all string // which are greater than given length k // using StringTokenizer static void findWords(String str, int K) { StringTokenizer st = new StringTokenizer(str); int count = 0 ; while (st.hasMoreTokens()) { // reading words String word = st.nextToken(); if (word.length() > K) { System.out.print(word + " " ); count++; } } if (count == 0 ) System.out.print( "No word is greater than length " + K); } // Driver code public static void main(String[] args) { String str = "neveropen for neveropen" ; int k = 4 ; findWords(str, k); } } // This code is contributed by codebrxnzt |
Python3
# Python program to find all string # which are greater than given length k # using split method def find_words(string, k): # split the string into words words = string.split() count = 0 # iterate through each word for word in words: # check if the length of the word is greater than k if len (word) > k: print (word, end = ' ' ) count + = 1 if count = = 0 : print (f "No word is greater than length {k}" ) # Driver code string = "neveropen for neveropen" k = 4 find_words(string, k) |
Javascript
// JavaScript program to find all string // which are greater than given length k function find_words(string, k) { // split the string into words let words = string.split( ' ' ); let count = 0; // iterate through each word for (let word of words) { // check if the length of the word is greater than k if (word.length > k) { console.log(word); count++; } } if (count === 0) { console.log(`No word is greater than length ${k}`); } } // Driver code let string = "neveropen for neveropen" ; let k = 4; find_words(string, k); |
C#
using System; using System.Collections.Generic; using System.Collections; using System.Linq; // C# program to find all string // which are greater than given length k class HelloWorld { public static void find_words( string str, int k) { // split the string into words string [] words = str.Split( ' ' ); int count = 0; // iterate through each word for ( int i = 0; i < words.Length; i++) { string word = words[i]; // check if the length of the word is greater than k if (word.Length > k) { Console.Write(word + " " ); count = count + 1; } } if (count == 0) { Console.WriteLine( "No word is greater than length " , k); } } static void Main() { // Driver code string str = "neveropen for neveropen" ; int k = 4; find_words(str, k); } } // The code is contributed by Nidhi goel. |
neveropen neveropen
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!