Page 358 - Cs_withBlue_J_C11_Flipbook
P. 358

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();





                356356  Touchpad Computer Science-XI
   353   354   355   356   357   358   359   360   361   362   363