Count words

Report a typo

Read an input text from the console and print the number of words. By word we mean a sequence of characters separated by one or several spaces.

If the input is empty or there are no characters except spaces, print 0.

Sample Input 1:

one two three

Sample Output 1:

3

Sample Input 2:

between   us  several   space characters

Sample Output 2:

5
Write a program in Java 17
import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// start coding here
reader.close();
}
}
___

Create a free account to access the full topic