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