Wednesday, January 16, 2008

Find the Longest Word in the String

import java.io.*;
class LongestWord
{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String");
String s=in.readLine();
int l=s.length();
String wo="",w="";
s=s+" ";
int max=0,x;
for(int i=0;i {
char ch=s.charAt(i);
if(ch!=' ')
{
wo = wo+ch;
}
else
{
x=wo.length();
if(x>max)
{
max=x;
w=wo;
}
wo="";
}
}
System.out.println("Longes Word is " + wo);
}
}

4 comments:

Unknown said...

The for loop is not complete. Please complete that

Unknown said...
This comment has been removed by the author.
Unknown said...

class Football
{
static void test(String st)
{
st=st+" ";
int l=st.length();
System.out.println("Entered String is "+st);
System.out.println("Length is : "+l);
int a = 0;
String bin="";
int maxSub=0;
for(int i=0;imax)
{
max=m[k];
maxSub=k;
}
}
System.out.println("The longest word is \'"+st2[maxSub]+"\' with maximum letters "+max);
}
}

This program is absolutely complete and works fine

Unknown said...
This comment has been removed by the author.