Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- Counts the number of words in a line.
- Input: a string containing the line
- Output: the number of words in the line
- */
- public class CountWords
- {
- public static void main(String[] args)
- {
- int count = 0;
- Scanner in = new Scanner(System.in);
- String input = in.nextLine();
- for(int i = 0; i < input.length(); i++)
- {
- if(" !?.".indexOf(input.charAt(i)) != -1){count++;}
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement