Program to find the LONGEST word in an entered String.

angad_aks's Avatar
Banned
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);
		}
		}
0
c_user's Avatar, Join Date: Aug 2009
Contributor
i think this post should not be in queries but in general java programming section.
still good code going brother.
0
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by angad_aks View Post
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);
        }
        }
  1. Comment your code...
  2. Document it..
  3. Make it more readable