2016-04-30 5 views
0

Ich brauche eine Hilfe. Ich habe eine Funktion, die längstes Wort im Satz druckt. Aber wie kürzestes Wort anzuzeigen?So finden Sie das kürzeste Wort in der Zeichenfolge C++

string text = "Mein Name ist Bob";

+0

diesen Code unter der Annahme korrekt ist man einfach austauschen müssen 'if (tmpWord.length()> maxWord.length()) maxWord = tmpWord;' 'mit if (tmpWord.length() user463035818

+0

Ich habe diese Variante ausprobiert. Leider funktioniert es nicht :( – TomRay

+1

warum es nicht funktioniert? Sie sollten Ihren Versuch und die Fehlermeldungen, die Sie erhalten, zeigen – user463035818

Antwort

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

Der Vorschlag im Kommentarbereich funktioniert, es ist nur eine Frage der Neuanordnung Ihrer Kontrollstrukturen, damit es funktioniert. d.h

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

} 

ich hinzufügen, können Sie nach einem Wort überprüfen viel leichter, wenn Sie istringstream mit der Extraktion operator>> verwendet. Etwas wie:

#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

Es wird immer einen kurzen und besten Weg geben, Dinge zu tun, ist nicht da !! +1 –

+0

1) Standardvergleich wird Zeichenfolgen lexikographisch vergleichen: "aaa" ist kleiner als "c". 2) "istream_iterator" ist ein Eingabe-Iterator und "min_element" benötigt mindestens einen Vorwärts-Iterator. –

+0

@Revolver_Ocelot Aktualisiert. – 0x499602D2

0

Wenn wir wollen beide erhalten min-Wert und Max-Wert, die initialize-Werte sollten Gegensätze zu jeder von ihnen sein. Eigentlich sollte das die Max-Limit-Zeichenkette 'Text' sein.
In der Entwicklung der Business-Anwendung ist dies ein gesunder Menschenverstand, aber einige Programmierer hassen den Weg dafür.

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