Write a program to accept a sentence. Display the word with maximum number of vowel.
import java.util.Scanner;
class maxvowel
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
String str=sc.nextLine()+" ";
String w="",mvw=""; //w word and mvw maximum vowel word
int mv=0; //maximum vowels
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
int nv=0; //number of vowels
for(int j=0;j<w.length();j++)
{
if("AaEeIiOoUu".indexOf(w.charAt(j))!=-1) //check for vowel
{
nv=nv+1; //increase the vowel count
}
}
if(nv>mv) //vowel count more than maximum vowel count
{
mv=nv;
mvw=w;
}
w=""; //clear the current word and to prepare for the next word
}
else
{
w=w+ch;
}
}
System.out.println("The word with maximum number of vowels: "+mvw);
}
}