Program to find the LONGEST word in an entered String.

Discussion in 'Java' started by angad_aks, Feb 15, 2011.

  1. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.io.*;
    class longwrd
    {
    	public static void main(String args[])throws IOException
    	{
    		String s,str;
    		char b;
    		int c=0,i,l,p=0,max=0;;
    		InputStreamReader x=new InputStreamReader(System.in);
    		BufferedReader y=new BufferedReader(x);
    		System.out.println("ENTER A STRING");
    		s=y.readLine();
    		s=s.concat(" ");
    		l=s.length();
    		for(i=0;i<l;i++)
    		{
    			b=s.charAt(i);
    			if(b!=' ')
    			{
    				c=c+1;
    			}
    			else
    			{
    				if(c>max)
    				{
    					max=c;
    					p=i;
    				}
    				c=0;
    			}
    		}
    		str=s.substring(p-max,p);
    		System.out.println(str+" "+max);
    		}
    		}
    
     
  2. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    i think this post should not be in queries but in general java programming section.
    still good code going brother.
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India

    1. Comment your code...
    2. Document it..
    3. Make it more readable
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice