Write a java program to enter a sentence and show the words with Maximum and minimum Vowels in it. In case of numbers of words with same feature, the first one will be displayed.
import java.util.Scanner;
class Star
{
String str1,s,maxs,mins;
int maxl=0,minl,j;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter the sentence:");
str1=sc.nextLine().trim();
str1=str1+ " ";
minl=str1.length();
while(true)
{
int i=str1.indexOf(' ');
if(i
break;
s=str1.substring(0,i);
j=countVowels(s);
if(j >=maxl)
{
maxl=j;
maxs=s;
}
if(j
{
minl=j;
mins=s;
}
str1=str1.substring(i+1);
}
System.out.println("\nWord With Maximum Vowels = "+maxs);
System.out.println("\nWord With Minumum Vowels = "+mins);
}
public int countVowels(String s)
{
s=s.toUpperCase();
int i,len,c=0;
len=s.length();
for(i=0;i
{
switch(s.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
c++;
break;
}
}
return c;
}
public static void main(String args[])
{
Star ob=new Star();
ob.take();
}
}
Sample Input and Output
Enter the sentence: This is a test on vowels counting
Word With Maximum Vowels = counting
Word With Minumum Vowels = This
This post first appeared on Tutorial Site On Computer Programming Languages F, please read the originial post: here