2016-04-30 58 views
0

我需要幫助。 我有一個打印Longest Word的函數。 但是如何顯示最短的單詞?如何查找字符串中的最短單詞C++

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; 
} 
+0

假設這代碼是正確你只需要交換'如果(tmpWord.length()> maxWord.length())maxWord = tmpWord;''與如果(tmpWord.length() user463035818

+0

我試過這個變種。不幸的是它不起作用:( – TomRay

+1

爲什麼它不起作用?你應該顯示你的嘗試和你得到的錯誤信息 – user463035818

回答

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

評論部分給出的建議將起作用,它只是重新安排控制結構以使其工作。即

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; 

    } 
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'; 
} 
+0

總是會有一個短暫的,最好的方式來做事情,是不是!! +1 –

+0

1)默認比較將按照字典順序比較字符串:「aaa」小於「c」。 2)'istream_iterator'是輸入迭代器,'min_element'至少需要轉發一個。 –

+0

@Revolver_Ocelot更新。 – 0x499602D2

0

如果我們想同時獲得最小值和最大值,初始化值應該是對立的他們每個人。 其實,這應該是'文本'的最大限制字符串。
在商業應用程序的開發中,這是常識,但一些程序員可能討厭這種方式。

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 = ""; 
    } 
} 
相關問題