SHARE
TWEET

Untitled

advictoriam Jan 11th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top