Wednesday, September 4, 2019

BlueJ Program To Find Word With Maximum And Minimum Vowels From A Sentence






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< 0)
 break;
 s=str1.substring(0,i);
 j=countVowels(s);
 if(j >=maxl)
 {
     maxl=j;
     maxs=s;
    }
    if(j< minl)
 {
     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< len;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

Related Post: BlueJ Programs on String/Sentence


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner