import java.util.Scanner;public class WordCount{public static void main(String[] args){Scanner in = new Scanner(System.in);System.out.print("Enter your string:");String str = in.next();countWords(str);System.out.print(countWords(str));}public static int countWords(String str){int count = 0;for (int i = 0; i < str.length()-1;="">{if ((str.charAt(i)==' ')&&(!(str.charAt(i+1)==' '))){count++;}}return count;}}
This is my code for counting the number of words in a string. This program compiles, but the answer is always 0. I don't know why the program is not considering the increment of count. I would appreciate help in this. Thanks!
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here