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
public static int wordCount(String s){
int counter=0;
for(int i=0;i<=s.length()-1;i++){
if(Character.isLetter(s.charAt(i))){
counter++;
for(;i<=s.length()-1;i++){
if(s.charAt(i)==' '){
i++;
break;
}
}
}
}
return counter;
}
This is what you need if don't use the predefined function. I have tested it by myself. Please let me know if there is any bugs!