使用stringstream查找大于给定长度k的单词

给定一个包含空格分隔的单词和数字K的字符串。任务是使用 C++中的StrugSuffs .

null

本文讨论了使用循环来解决这个问题的一般解决方案 前一篇文章 在本文中,将讨论使用C++中的StrugSuffic的解决方案。

例子 :

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

其思想是使用stringstream创建一个流,方法是将给定的字符串拆分为标记,然后处理流并打印长度大于K的单词。

以下是上述理念的实施情况:

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


输出:

geeks geeks

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享