actually, you can do it using the fucntions of the String class. Here's my take:
import java.util.Scanner;
class test
{
static Scanner sc = new Scanner(System.in);
public static void main()
{
String st = sc.nextLine()+" ";
int l=0;
for(int i=0; i<st.length(); i++)
{
String s = st.substring(i,st.indexOf(' ',i));
if(s.length()>st.substring(l,st.indexOf(' ',l)).length())
l = i;
}
System.out.println("Longest word is "+st.substring(l,st.indexOf(' ',l)));
}
}
Let me know if you have any questions
benjamin_jobilal_y
|