我需要帮助。我有一个在句子中打印最长单词的功能。但是如何显示最短的单词呢?
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;
}