如何找到字符串C++中最短的单词

How to find the shortest word in the string C++

本文关键字:单词 C++ 何找 字符串      更新时间:2023-10-16

我需要帮助。我有一个打印句子中最长单词的功能。但是如何显示最短的单词呢?

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 = "";
    }
}