Find words which are greater than given length k using stringstream

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 zambiatek for zambiatek
is computer science portal"
K = 4
Output : hello zambiatek zambiatek 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 stringstreamvoid 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 codeint main(){Â Â Â Â string str = "zambiatek for zambiatek";Â Â Â Â Â Â Â Â Â int k = 4;Â
    findWords(str, k);         return 0;} |
Java
// Java program to find all string// which are greater than given length k// using StringTokenizerimport 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 = "zambiatek for zambiatek";    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 codestring = "zambiatek for zambiatek"k = 4find_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 codelet string = "zambiatek for zambiatek";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 = "zambiatek for zambiatek";        int k = 4;        find_words(str, k);    }}Â
// The code is contributed by Nidhi goel. |
Output
zambiatek zambiatek
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


