OneCompiler

Input a sentence and display the LONGEST WORD.

198

Write a program to input a sentence and display the longest word.

import java.util.*;

public class LONGESTword
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String S = sc.nextLine();
S=S+"";
String w, wL="";
int i1;
i1=0; w="";
int len = S.length();
char c;
for(int i = 0; i<len;i++)
{
c=S.charAt(i);
if(c!=' ')
w=w+c;
else
{
if(w.length()>i1)
{
wL=w;
i1 = w.length();
}
w="";
}
}
System.out.println("LONGEST WORD IS : "+ wL);
System.out.println("LENGTH IS : " + i1);
}
}

FOR CODE,
CLICK : https://onecompiler.com/java/3xympbbte ( TO FIND OUTPUT )