Open In App

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 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: 

Implementation:




// 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;
}




// 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 = "geeks for geeks";
    int k = 4;
 
    findWords(str, k);
  }
}
 
// This code is contributed by codebrxnzt




# 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 = "geeks for geeks"
k = 4
find_words(string, k)




// 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 = "geeks for geeks";
let k = 4;
find_words(string, k);




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 = "geeks for geeks";
        int k = 4;
        find_words(str, k);
    }
}
 
// The code is contributed by Nidhi goel.

Output
geeks geeks 

Article Tags :