0

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

string text = "我叫鲍勃";

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

4 回答 4

1

评论部分给出的建议会起作用,只需重新安排控制结构以使其起作用。IE

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我可能会补充一点,如果您与 extract 一起使用,您可以很容易地检查单词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;

    }
于 2016-04-30T18:37:06.390 回答
1
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';
}
于 2016-04-30T18:46:35.977 回答
0

If we want to get both min value and max value, the initialize values should be opposites to each of them. Actually, that should be the max-limit string of 'text'.
In the development of the business application, this is common sense, but some programers may hate the way of this.

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 = "";
    }
}
于 2016-04-30T19:10:38.807 回答
0
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;
}
于 2016-04-30T18:39:36.077 回答