The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
import java.util.Scanner;
public class task4
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter some text, or q to quit: ");
String text = in.nextLine();
if (text.equals("q"))
{
System.out.printf("Exiting...n");
break;
}
int result = count_words(text);
System.out.printf("Counted %d words.nn", result);
}
}
}
This is an incomplete program. The goal of the program is to take as input sentences from the user, and then print out the number of words in each sentence. Complete that program, by defining acount_wordsfunction, that satisfies the following specs:
The strategy for counting words is simple:
It may be that the text starts with multiple spaces, or that multiple spaces are placed between two words. Your code should handle that correctly.
IMPORTANT: you are NOT allowed to modify in any way the main function.
This is an example run of the complete program:
Enter some text, or q to quit: this is the fourth week of the semester Counted 8 words. Enter some text, or q to quit: hello world Counted 2 words. Enter some text, or q to quit: Counted 0 words. Enter some text, or q to quit: h hhg Counted 2 words. Enter some text, or q to quit: q Exiting...