using Dr java Objective: Write a program that takes a phrase and then counts the
ID: 3792351 • Letter: U
Question
using Dr java
Objective:
Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels.
Hint:
It may be a good idea to keep track and sort two arrays:
Has the vowels in alphabetic order
Has the number of said vowels
Whenever one would swap then it swaps the values in both arrays.
Example Dialog:
Welcome to the vowel counter and sorter!
Enter a phrase!
aaaaaeeeeiiioou
The vowels and their count
u 1
o 2
i 3
e 4
a 5
Another Example
Welcome to the vowel counter and sorter!
Enter a phrase!
Facetious is the only word in English that contains all vowels in order!
The vowels and their count
u 1
a 4
e 5
i 6
o 6
Yet another Example
Welcome to the vowel counter and sorter!
Enter a phrase!
I have a pen, I have an apple. Uh! Apple Pen I have a pen, I have pineapple. Uh! Pineapple Pen Apple pen... Pineapple pen... uh! Pen Pineapple Apple Pen! Pen Pineapple Apple Pen
The vowels and their count
o 0
u 3
i 9
a 17
e 29
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Namburi Ramesh
*/
public class VowelCounter {
public static void main(String [] args){
int[] count=new int[5]; //to store the count of vowels
char[] vowels=new char[]{'a','e','i','o','u'}; // to store the associated vowels
String phrase; //to store the entered phrase
Scanner scanner = new Scanner(System.in); //to take the input
System.out.println("Welcome to the vowel counter and sorter! " +"Enter a phrase!");
phrase=scanner.nextLine(); // stores input string into phrase variable
/*
Run a loop to count the number of vowels in the entered string and store it in the count array
*/
for(int j=0;j<phrase.length();j++){
if(phrase.charAt(j) == 'a' || phrase.charAt(j)=='A'){
count[0]++;
}else if(phrase.charAt(j)=='e' || phrase.charAt(j)=='E'){
count[1]++;
}else if(phrase.charAt(j)=='i' || phrase.charAt(j)=='I'){
count[2]++;
}else if(phrase.charAt(j)=='o' || phrase.charAt(j)=='O'){
count[3]++;
}else if(phrase.charAt(j)=='u' || phrase.charAt(j)=='U'){
count[4]++;
}
}
System.out.println("The vowels and their count");
int maximum=count[findmax(count)]; // find the maximum element in the count array
/*
loop to print the number of vowels in ascending order
*/
for(int j=0;j<5;j++){
int nextmin=findmin(count); // find the position of minimum element in array count
System.out.println(vowels[nextmin] +" "+ count[nextmin]); //print the associated vowel and count
count[nextmin]=maximum+1; // set the minimum element to more than maximum so that it wont be returned when we next call the finfmin method
}
}
//Below function returns the position of minimum element in the array a
public static int findmin(int[] a){
int minposition=0;
for(int i=1;i<a.length;i++){
if(a[minposition]>a[i]){
minposition=i;
}
}
return minposition;
}
//Below function returns the position of maximum element in the array a
public static int findmax(int[] a){
int maxposition=0;
for(int i=1;i<a.length;i++){
if(a[maxposition]<a[i]){
maxposition=i;
}
}
return maxposition;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.