Write a program to accept a sentence. Find the shortest word present in the sentence.
import java.util.Scanner;
class shortestWord
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
String str=sc.nextLine()+" ";
int min=99999;
String w="",sw=""; //w for word
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
if(w.length()<min)
{
min=w.length();
sw=w;
}
w="";
}
else
{
w=w+ch;
}
}
System.out.println("The word with shortest length is: "+sw);
}
}