Question: Java Help!!!! Write a method that allows counting words in a string: int wordCount(String s) {... Write a main program that asks the user for

Java Help!!!! Write a method that allows counting words in a string:

int wordCount(String s) {...

Write a main program that asks the user for an input string, calls your method on the string, and prints out the number of words in the string without using predefined methods of the String class.

The assumption is that words are separated by spaces, however, not necessarily a single space. Counting spaces will not always give the correct answer. You have to detect groups of spaces and count them as a single separator. "Hello world" with one space and "Hello world " with ten spaces between the words and 3 trailing spaces at the end must both yield same answer: 2 words. Do not use any predefined methods of the String class. You need (a) loop(s) and (an) if-statement(s) to write your own logic.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!