how to count the exact number of words in a string that has empty spaces between words?
<p>Write a method called wordCount that accepts a String as its parameter and returns the number of words in the String. A word is a sequence of one or more nonspace characters (any character other than ' '). For example, the call wordCount("hello") should return 1, the call wordCount("how are you?") should return 3, the call wordCount(" this string has wide spaces ") should return 5, and the call wordCount(" ") should return 0. </p>

<p>I made a function--</p>

<pre><code>public static int wordCount(String s){
 int counter=0;
 for(int i=0;i&lt;=s.length()-1;i++){
         if(Character.isLetter(s.charAt(i))){
                 counter++;
                 for(;i&lt;=s.length()-1;i++){
                         if(s.charAt(i)==' '){
                                 counter++;
                                 }
                         }

                 }


         }
         return counter;
}
</code></pre>

<p>But i know this has 1 limitation that it will also count the number of spaces after all the words in the string have finished nad it will also count 2 blank spaces as possibly being 2 words :(
Is there a predefined function for word count? or can this code be corrected?</p>
