COMP 248 Lecture 1: Sentence.java

128 views1 pages

Document Summary

* assignment #2 - question 1 - one possible solution. // let"s initialize the various lenghts maxlen=0; minlen=sentence. length(); curlen=0; sumwordlen=0; System. out. println("\n\nhere are some stats on your sentence: \n"); System. out. println("word " + (nbwords+1) + " has " + curlen + " characters. // check if it is the longest or shortest word so far if (curlen > maxlen) maxlen = curlen; if (curlen < minlen) minlen = curlen; // increase the the sum of all lengths to compute the average later sumwordlen += curlen; // reset the lenght of the current word to zero for the next word to come curlen = 0; else // we are still "inside" a word curlen++; // count one more character in the word. // once all characters have been processed, let"s display the results. System. out. println("\nthere are " + nbwords + " words. System. out. println("the longest word has " + maxlen + " characters. System. out. println("the shortest word has " + minlen + " characters.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents