Find Words which are Greater than given Length k using Stringstream


This is the problem based on stringstream class present in C++ “sstream” header file. Here, we have to find those strings whose lengths are greater than “k”. This task will be performed by using stringstream class. The concept is to partition the string and then iterate through the defined words. The value of length k has to be specified to obtain the words which are larger than k whereas the length of the words which are smaller than k will not be displayed in the output. In this article, we will understand how to find words which are greater than given length k using stringstream class.

To show you some instances

If,

Line = “What a Fantastic Day”

K = 3

Then,

Words having a length greater than 3 are: {What, Fantastic}

If,

K = 5

Then,

Words having a length greater than 5 are: {badminton, tournament}

Syntax

Use stringstream object declaration as defined below-

stringstream input(line);:

where “input” is the object, and the “line” is the string of words.

To obtain the string of multiple words, use the getline() inbuilt function.

Below represents the syntax of it-

getline(cin, line);

Multiple Approaches

We have provided the solution in different approaches.

  • Solution based on Iterative Algorithm.

  • Solution based on Recursive Algorithm.

Let’s implement the program along with its output one by one.

Approach-1: Solution based on Iterative Algorithm

This is an Iterative approach to solving the problem. Here, we are using the while() loop for doing the traversal among the words present in the string. The while loop breaks only after the end of strings in the “stringstream” stream.

Algorithm: findWordsGreaterThanK(line, K)

Step-1: Create stringstream object named “input” initialized with “line”.

Step-2: Create the “WORD” variable of the type string.

Step-3: Read in “WORD” till any word is present in the “input” object.

Step-4: If (length(WORD) > K), THEN PRINT(WORD).

Step-6: EXIT.

Example

#include <iostream>
#include <sstream>
using namespace std;
/**
* Iterative method for finding the words having a length greater than K
*
* @param line
* @param K
*/
void findWordsGreaterThanK(string line, int K) {
   // Create stringstream object named input
   stringstream input(line);

   string WORD;

   // display the result
   cout << "Words whose length is greater than " << K << ":\n";

   while (input >> WORD) {
       // if word length is greater than K
       if (WORD.length() > K) {
           cout << WORD << endl;
       }
   }
}
int main() {
   string line;
   int K;
   // get a line input from the user
   cout << "Enter Line (words separated by SPACE): ";
   getline(cin, line);  
   // get K from the user
   cout << "Enter K: ";
   cin >> K;
   // call the iterative method
   findWordsGreaterThanK(line, K);
   return 0;
}

Output

Enter Line (words separated by SPACE): What 
a pleasant day
Enter K: 2
Words whose length is greater than 2:
What
pleasant
day

Time Complexity of Program = O(N)

Space Complexity of Program = O(N)

Approach-2: Solution based on Recursive Algorithm

This is a Recursive Approach based on the recursive algorithm. The base case for the end of recursion is the empty stringstream object.

Algorithm: displayWordsGreaterThanK(input, K)

Step-1: Create the “WORD” variable of the type string.

Step-2: Read in “WORD” from the “input” object.

Step-3: If WORD == empty == “”, THEN return.

Step-4: IF length(WORD) > K, THEN print(WORD).

Step-5: Recursively CALL displayWordsGreaterThanK(input, K).

Example

#include <iostream>
#include <sstream>
using namespace std;
/**
* Recursive method for displaying the words having a length greater than K
*
* @param input
* @param K
*/
void displayWordsGreaterThanK(stringstream &input, int K) {
   string WORD;
   input >> WORD;

   if (WORD == "") {
       return;
   }
   if (WORD.length() > K) {
       cout << WORD << endl;
   }
   displayWordsGreaterThanK(input, K);
}
int main() {
   string line;
   int K;
   // get a line input from the user
   cout << "Enter Line (words separated by SPACE): ";
   getline(cin, line);
   // get K from the user
   cout << "Enter K: ";
   cin >> K;
   // Create stringstream object named input
   stringstream input(line);
   // display the result
   cout << "Words whose length is greater than " << K << ":\n";
   // call the recursive method
   displayWordsGreaterThanK(input, K);

   return 0;
}

Output

Enter Line (words separated by SPACE): 
Alias won first prize in the Cricket Tournament
Enter K: 5
Words whose length is greater than 5:
Cricket
Tournament

Time Complexity of Program = O(N)

Space Complexity of Program = O(N)

In this article, we illustrated two different approaches named iterative and recursive methods to obtain words that are larger than the specified length.

Updated on: 23-Aug-2023

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements