numWords
Category: Programming
Author: Stuart Reges
Book Chapter: 4.3
Problem: numWords
Write a method numWords that takes a String as a
parameter and that returns the number of words in the String. By
definition, words are separated by one or more spaces. For example:
numWords("how many words here?")
should return 4. Notice that words can contain punctuation marks. Any
non-empty sequence of non-space characters can be a word. There might be
spaces at the beginning or end of the String. For example:
numWords(" how about merry-go-round and !&$%--$$!!*() ")
should return 5. You may not construct any other objects to solve this
problem (e.g., you can't use a Scanner or tokenizer). You may assume that
the String has no other whitespace characters such as tabs or newline
characters. Your method has to pay attention only to spaces to decide how
many words there are.
Write your solution to numWords below.