Question:
The following string objects are known:
String line1 = "we were her pride of 10 she named us :";
String line2 = "Benjamin, Phoenix, the prodigal ";
String line3 = "and perspicacious Pacific Suzanne ";
String sentence = line1 + ''+ line2 +'' + line3;
Compile a program to calculate the number of words in sentence and point out the longest and shortest words. If there are multiple longest or shortest words, all of them are output.
An error was found in the reference program of the original book:
1. When the while loop misses the subscripts auto-increment statement, the program can be infinitely looping.
2. The program does not take into account the consecutive line2 and space conditions, resulting in the query of the shortest word is a space character.
3. The program does not consider that the starting and ending subscripts of the first word "we" are 0 and 3. When the while loop is executed for the first time, it has started to find the first space position, the word "we" is not calculated by the program.
Modify program:
// If the following String object is known, write a program to calculate the number of words in sentence and specify the longest and shortest words. If there are multiple longest or shortest words, all of them are output. # Include <iostream> # include <string> # include <vector> using namespace STD; int main () {string line1 = "we were her pride of 10 she named us :"; string line2 = "Benjamin, Phoenix, the prodigal"; string line3 = "and perspicacious Pacific Suzanne"; string sentence = line1 + ''+ line2 +'' + line3; string separators ("\ t:, \ v \ r \ n \ f"); // string word used as the separator; vector <string> longestwords, shortestwords; // The vector that stores the longest and shortest words Container string: size_type maxlen, minlen, wordlen, Count = 0; // the longest and shortest word in sentence and the length of the next word. Number of words: String: size_type startpos = 0, endpos = 0; // start and end of a word endpos = sentence. find_first_of (separators, endpos); // obtain the first word if (endpos = string: NPOs) // The next separator location cannot be found, that is, the string has only one word wordlen = sentence. size ()-startpos; elsewordlen = endpos-startpos; word. assign (sentence. begin () + startpos, sentence. begin () + startpos + wordlen); // obtain the word startpos = s Entence. find_first_not_of (separators, endpos); // set the start position of the next search maxlen = minlen = wordlen; // read the first word, and the longest and shortest value is longestwords. push_back (Word); shortestwords. push_back (Word); ++ count; // process a word while (startpos = sentence. find_first_of (separators, endpos ))! = String: NPOs) // locate the start position of the next word {++ startpos; endpos = sentence. find_first_of (separators, startpos); // locate the end position of the next word if (endpos-startpos) = 0) // determine whether consecutive characters are obtained for separation; else {++ count; If (endpos = string: NPOs) // The next separator location cannot be found, that is, the word is the last word wordlen = sentence. size ()-startpos; elsewordlen = endpos-startpos; word. assign (sentence. begin () + startpos, sentence. begin () + startpos + wordlen); // obtain the word startpos = sentence. find_first_not_of (separa Tors, endpos); // set the start position of the next query if (wordlen> maxlen) {// The current word is longer than the current longest word. maxlen = wordlen; longestwords. clear (); // clear the container longestwords that stores the longest words. push_back (Word);} else if (wordlen = maxlen) // longestwords of the current word and the current longest word. push_back (Word); If (wordlen <minlen) {// The current word is shorter than the current longest word minlen = wordlen; shortestwords. clear (); // clear the shortestwords container that stores the shortestwords. push_back (Word);} else if (wordlen = minlen) // The shortestwords of the current word and the current shortestwords. push_back (Word );}} // Number of output words cout <"word amount:" <count <Endl; vector <string>: iterator ITER; // output the longest word cout <"longest word (s):" <Endl; iter = longestwords. begin (); While (ITER! = Longestwords. end () cout <* ITER ++ <Endl; // output the shortest word cout <"shortest word (s):" <Endl; iter = shortestwords. begin (); While (ITER! = Shortestwords. End () cout <* ITER ++ <Endl; return 0 ;}
Program running result: