import javautilScanner public class CountWords public stati

import java.util.Scanner;

public class CountWords

{

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...\ \");

break;

}

int result = countWords(text);

System.out.printf(\"Counted %d words.\ \ \", result);

}

}

}

Complete the 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 a countWords function, that satisfies the following specs:

-Function countWords takes one argument, called text, which is a string, and which is allowed to contain only uppercase letters, lowercase letters, and the space character \' \'. In other words, don\'t worry about handling cases cases where text contains characters that are punctuation, special symbols, or anything else that is not a letter or the space character.

-Function countWords counts and returns the number of words in text.

The strategy for counting words is simple:

- You initialize your counter to zero if your text starts with a space character, or to 1 if your text starts with a letter.

- You go through the text, character by character. You increment your counter every time you find a letter whose previous character was space. For example, if your current character is \'m\' and the previous character was \' \', it means that you have found a new word.

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...

Solution

CountWords.java

import java.util.Scanner;

public class CountWords
{
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...\ \");
break;
}
int result = countWords(text);
System.out.printf(\"Counted %d words.\ \ \", result);
}
}
public static int countWords(String text){
   int counter = 0;
   boolean isSpace = false;
   if(text.length()>0){
       counter = 1;
   }
   for(int i=0; i<text.length();i++){
       char ch = text.charAt(i);
       if(isSpace){
           if(ch!=\' \'){
               counter++;
           }
           isSpace=false;
       }
       if(ch == \' \'){
           isSpace=true;
       }
   }
  
   return counter;
}

}

Output:

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...

import java.util.Scanner; public class CountWords { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { System.out.prin
import java.util.Scanner; public class CountWords { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { System.out.prin
import java.util.Scanner; public class CountWords { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { System.out.prin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site