Java 使用Stringstream查找长度大于给定长度k的单词
这是基于C++中“sstream”头文件中的stringstream类的问题。在这里,我们要找到那些长度大于“k”的字符串。这个任务将使用stringstream类来完成。我们的思路是对字符串进行分割,然后遍历定义的单词。必须指定长度k的值,以获取大于k的单词,而长度小于k的单词将不会显示在输出中。在本文中,我们将了解如何使用stringstream类查找长度大于给定长度k的单词。
示例
如果,
Line = “What a Fantastic Day”
K = 3
那么,
长度大于3的单词为:{What, Fantastic}
如果,
K = 5
那么,
长度大于5的单词为:{badminton, tournament}
语法
使用以下方式声明stringstream对象-
stringstream input(line);:
其中“input”是对象,而“line”是一串单词的字符串。
要获取多个单词的字符串,使用内置函数getline()。
以下是其语法表示-
getline(cin, line);
多种方法
我们提供了不同的方法来解决问题。
- 基于迭代算法的解决方案。
-
基于递归算法的解决方案。
让我们逐个实现程序及其输出。
方法-1:基于迭代算法的解决方案
这是一种迭代方法来解决问题。在这里,我们使用while()循环来遍历字符串中的单词。只有当在“stringstream”流中遍历完字符串后,while循环才会退出。
算法:findWordsGreaterThanK(line, K)
步骤1 : 创建名为“input”的stringstream对象并初始化为“line”。
步骤2 : 创建名为“WORD”的字符串类型变量。
步骤3 : 读取“WORD”,直到“input”对象中不存在任何单词。
步骤4 : 如果(LENGTH(WORD) > K),则打印(WORD)。
步骤6 : 退出。
示例
#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;
}
输出
Enter Line (words separated by SPACE): What
a pleasant day
Enter K: 2
Words whose length is greater than 2:
What
pleasant
day
程序的时间复杂度: = O(N)
程序的空间复杂度: = O(N)
方法-2:基于递归算法的解决方案
这是一个基于递归算法的递归方法。递归结束的基本情况是空的stringstream对象。
算法:displayWordsGreaterThanK(input, K)
步骤-1: 创建类型为string的“WORD”变量。
步骤-2: 从“input”对象中读取“WORD”。
步骤-3: 如果WORD empty “”,则返回。
步骤-4: 如果length(WORD)> K,则打印(WORD)。
步骤-5: 递归调用displayWordsGreaterThanK(input,K)。
示例
#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;
}
输出
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
程序的时间复杂度 = O(N)
程序的空间复杂度 = O(N)
在本文中,我们介绍了两种不同的方法,即迭代和递归方法,用于获取长度大于指定长度的单词。