The Wayback Machine - https://web.archive.org/web/20240118184213/https://www.geeksforgeeks.org/find-words-which-are-greater-than-given-length-k-using-stringstream/
Open In App
Related Articles

Find words which are greater than given length k using stringstream

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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




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




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


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


Output

geeks geeks 

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads