Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.37 KB | None | 0 0
  1. import java.io.*;
  2. class sa
  3. {
  4.     public static void main () throws IOException
  5.     {
  6.         int count =0;
  7.         int count1=0;
  8.         BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
  9.         System.out.println ("Enter a sentence");
  10.        
  11.         String sent = br.readLine();
  12.        
  13.         String sent1 = sent + " ";
  14.         int l = sent1.length();
  15.         for (int a =0; a<l; a++)
  16.         {
  17.             if (sent1.charAt(a) == ' ')
  18.             {
  19.                 count ++;
  20.             }
  21.         }
  22.         String sentarr[] = new String[count];
  23.        
  24.        
  25.         boolean isSpace = false;
  26.         for (int c=0; c<l; c++)
  27.         {
  28.             char x= sent1.charAt(c);
  29.             if (x != ' ')
  30.             {
  31.                 if(isSpace == true)
  32.                 {
  33.                     isSpace = false;
  34.                     sentarr[count1] = x;
  35.                 }
  36.                 else
  37.                 {
  38.                     sentarr[count1] = sentarr[count1] + x;
  39.                 }
  40.             }
  41.             if (x == ' ')
  42.             {
  43.                 count1++;
  44.                 isSpace = true;
  45.             }
  46.         }
  47.         System.out.println ("Number of words =" + count);
  48.         for (int d=0; d<count; d++)
  49.         {
  50.             System.out.println ("At index number" + d + " word is " + sentarr[d]);
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement