Page 333 - computer science (868) class 11
P. 333

8          {
                   9              Scanner sc=new Scanner(System.in);

                  10              System.out.println("Enter a sentence");
                  11              sen=sc.nextLine();

                  12              len=0;
                  13          }

                  14
                  15          void printlong()
                  16          {

                  17              //Creating object of StringTokenizer class

                  18              // and defining  space , . ? as delimeters
                  19              StringTokenizer st=new StringTokenizer(sen," ,.?");
                  20              StringTokenizer st1=new StringTokenizer(sen," ,.?");

                  21              String w;
                  22              int l;

                  23              while(st.hasMoreTokens())  //Checking for words
                  24              {

                  25                  w=st.nextToken();  //Extracting words
                  26                  l=w.length();  // finding length of each word

                  27                  len=Math.max(len,l); //finding word with highest length
                  28              }

                  29              // st1 object is used as we have reached the end of st object
                  30              System.out.println("Longest word is");

                  31              while(st1.hasMoreTokens()) //Checking for word
                  32              {

                  33                  w=st1.nextToken();  //Extracting words
                  34                  if(w.length()==len)  // Checking words with maximum length

                  35                  System.out.println(w); //printing words with highest length
                  36              }

                  37          }
                  38

                  39          public static void main()
                  40          {
                  41              Longest ob=new Longest();





                                                                                                                       331
                                                                                                    Basic Input/Output   331
   328   329   330   331   332   333   334   335   336   337   338