Advertisement
advictoriam

Untitled

Jan 11th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.    Counts the number of words in a line.
  5.    Input: a string containing the line
  6.    Output: the number of words in the line
  7. */
  8. public class CountWords
  9. {
  10.    public static void main(String[] args)
  11.    {
  12.       int count = 0;
  13.  
  14.       Scanner in = new Scanner(System.in);
  15.       String input = in.nextLine();
  16.      
  17.       for(int i = 0; i < input.length(); i++)
  18.       {
  19.          if(" !?.".indexOf(input.charAt(i)) != -1){count++;}
  20.       }
  21.  
  22.       System.out.println(count);
  23.    }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement