Programming Examples
Java program to find the largest and smallest word in sentence
Write a Java Program to find the Largest and Smallest word in String.
import java.util.*;
class LargestWord
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Any String ");
String str=sc.nextLine();
String max="",min="",word="";
str=str+" ";
min=str;
for(int a=0;a<str.length();a++)
{
if(str.charAt(a)==' ')
{
if(max.length()<word.length())
{
max=word;
}
if(min.length()>word.length())
{
min=word;
}
word="";
}
else
{
word=word+str.charAt(a);
}
}
System.out.println("\n Largest Word : "+max);
System.out.println("\n Smallest Word : "+min);
}
}
Output
Enter Any String
ram is a good boy
Largest Word : good
Smallest Word : a