如何找到字符串C++中最短的单词
How to find the shortest word in the string C++
我需要帮助。我有一个打印句子中最长单词的功能。但是如何显示最短的单词呢?
string text="我叫Bob";
void LongestWord(string text)
{
string tmpWord = "";
string maxWord = "";
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
tmpWord = "";
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord=tmpWord;
}
cout << "Longest Word: " << maxWord << endl;
cout << "Word Length: " << maxWord.length() << endl;
}
注释部分给出的建议会起作用,只是重新安排控制结构以使其起作用。即
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
{
if(minWord.length()==0)//this only happens once
minWord=tmpWord;//for the first word,you need to assign minWord so you have something to compare to
if(tmpWord.length() < minWord.length() )//move this block here
minWord=tmpWord;
tmpWord = "";
}
}
我可以补充一点,如果您将istringstream
与提取operator>>
一起使用,您可以很容易地检查单词。类似于:
#include <sstream>
....
string text="my name is bob";
string tmpWord = "";
string minWord = "";
istringstream ss(text);//defines the input string stream and sets text in the input stream buffer
while(ss.peek()!=EOF)//until the end of the stream
{
ss>>tmpWord;//read a word up to a space
if(minWord.length()==0)//this only happens once
minWord=tmpWord;
if(tmpWord.length() < minWord.length() )
minWord=tmpWord;
}
void ShortestWord(std::string const& text)
{
std::stringstream ss(text);
std::vector<std::string> v(std::istream_iterator<std::string>(ss), {});
auto min = std::min_element(v.begin(), v.end(),
[] (auto& lhs, auto& rhs) { return lhs.size() < rhs.size(); });
auto p = std::make_pair(*min, min->size());
std::cout << "Shortest Word: "" << p.first << ""n";
std::cout << "Word Length: " << p.second << 'n';
}
void ShortestWord(string text)
{
string tmpWord = "";
// The upper bound of answer is text
string minWord = text;
for(int i=0; i < (int)text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
{
tmpWord += text[i];
}
else
{
// We got a new word, try to update answer
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
tmpWord = "";
}
}
// Check the last word
if(tmpWord != "")
{
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
}
cout << "Shortest Word: " << minWord << endl;
cout << "Word Length: " << minWord.length() << endl;
}
如果我们想同时获得最小值和最大值,初始化值应该与它们相反。实际上,这应该是"text"的最大限制字符串
在业务应用程序的开发中,这是常识,但有些程序员可能讨厌这种方式。
string minWord = text; // MAX_SIZE
string maxWord = "";
for(int i = 0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
if(text[i] == ' ' || i == text.length()) {
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord = tmpWord;
if(tmpWord.length() < minWord.length())
minWord = tmpWord;
tmpWord = "";
}
}
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- C++为一串单词添加空格
- 在C++中查找(奇怪的)字符串中的单词
- 当字符串是某个单词时给出输出?
- 当您在此单词中搜索单词时调整字符数组的大小?
- 数每个单词的元音
- 我们如何在文本文件中找到C++中的几个单词?
- 使用 BFS 的单词梯 2
- 替换字符串中的单词,但忽略引号中的单词