Search This Blog

Saturday 15 October 2016

to find longest word in a sentence

import java.io.*;
class longest_word
{
   public static void longest(  ) throws IOException
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("enter the string ");
    String str=br.readLine();
     str=str.trim();
     str=str+" ";
     int l = str.length();
     String max="",temp=" ";
     int maxl=0,tl=0            ,p=0;
  
     for (int i=0;i<l;i++)
     {
        if(str.charAt(i)==32)
        {
            temp=str.substring(p,i);
            tl=temp.length();
       
            if( tl > maxl )
         
                max=temp;
                maxl=tl;
           
         p=i+1;
    }
      
  
    }
      System.out.println("the longest word is"+max);
     System.out.println("the lenght of the longest word is"+maxl);
 }
}

No comments:

Post a Comment

Make a JAVA program for a Toystore. This program suggest the toy based on the inputs users gives like age , gender , etc

 //editor used - IntelliJ IDEA package ToyStore; import javax.swing.*; import java.awt.event.*; import java.util.Scanner; //here you can edi...