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

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

我需要帮助。 我有一个函数可以在句子中打印最长的单词。 但是如何显示最短的单词?

字符串文本="My name is 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(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;
}
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';
}

如果我们想同时获得最小值和最大值,初始化值应该与它们中的每一个相反。 实际上,那应该是 '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 = "";
    }
}