Program opened below. You can scroll through the algorithm, Java code and output.

Words with More Than Two Vowels

Download .java

Question

Read a sentence and list only words having more than two vowels, in increasing order of their vowel counts. Preserve original order when counts tie.

Step 1: Start and read the sentence. Step 2: Break the sentence into individual words. Step 3: Prepare arrays to store qualifying words and their vowel counts. Step 4: Initialize the qualifying-word count to zero. Step 5: Define a function that scans a word. Step 6: Convert each letter to uppercase during vowel testing. Step 7: Count occurrences of A, E, I, O and U. Step 8: Return the vowel count to the main function. Step 9: Visit every input word in its original sequence. Step 10: Keep words only when they contain at least three vowels. Step 11: Store the word and vowel count at matching array positions. Step 12: Apply insertion sort using vowel count as the comparison key. Step 13: Shift only entries with strictly larger counts to preserve ties. Step 14: Display each chosen word together with its vowel count. Step 15: Stop.
Java
import java.util.*;
class VowelWordOrder {
    static int count(String s) {
        int total = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = Character.toUpperCase(s.charAt(i));
            if ("AEIOU".indexOf(ch) >= 0) {
                total++;
            }
        }
        return total;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input = sc.nextLine().trim().split(" +");
        String[] selected = new String[input.length];
        int[] totals = new int[input.length];
        int size = 0;
        for (String word : input) {
            int vowels = count(word);
            if (vowels > 2) {
                selected[size] = word;
                totals[size++] = vowels;
            }
        }
        for (int i = 1; i < size; i++) {
            String key = selected[i];
            int total = totals[i];
            int j = i - 1;
            while (j >= 0 && totals[j] > total) {
                selected[j + 1] = selected[j];
                totals[j + 1] = totals[j];
                j--;
            }
            selected[j + 1] = key;
            totals[j + 1] = total;
        }
        for (int i = 0; i < size; i++) {
            System.out.println(selected[i] + " : " + totals[i]);
        }
    }
}
Input: education aeon idea Output: aeon : 3 / idea : 3 / education : 5
← Previous Program 27 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.