Question
Write the countWords method that returns the number of words in the text, where a word as any string separated by one space (you are
Write the countWords method that returns the number of words in the text, where a word as any string separated by one space (you are not expected to handle multiple spaces between words). For example: countWords( "Along came a spider and sat down beside her") returns 9 because nine words appeared in the text. Note the following: Your method returns 0 if presented with the empty string; You may use only the String methods used in class: charAt( int ), indexOf( char)
and/or length(); Assume that text does not begin nor does it end with any spaces; spaces appear only between words; Your method can use at most one Java iteration statement (either a for, a while
or a do-while statement); and Your method may go through the text only once. static int countWords( String text ) { String space = " "; int wordCount = 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started