Java Program to Count the Frequency of each vowel in the String

Published by user on

In this article, we will look at how to count the frequency of each vowel in Java. Our goal is to count the number of ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, and also their lower case forms.

In the program, we will maintain an array that keeps the count of each vowel.

Logic

  1. Declare an int array ‘counters’ of size five, as we have five vowels
  2. Consider  counters[0] represents count of ‘A’ or ‘a’, counters[1] represents count of ‘E’, or ‘e’ and so on and so forth
  3. Suppose the program encounters ‘A’ or ‘a’ then the counters[0] is incremented. If the program encounters ‘E’ or ‘e’ then counters[1] is incremented. Similar logic applies to the rest of the vowels
  4. Perform Step 1 through Step 3 for each character of the given String

Program to count vowels

As we saw the logic in the previous section, now let’s look at the program.

// Java program to demonstrate
// how to count the frequency
// of each vowel
public class VowelFrequencyCounter {
        public static void main(String[] args) {
                String str = "You are using devcubicle!!!";

                // allocate space for 5 vowels
                int[] counters = new int[5];

                for (int i = 0; i < str.length(); i++) {
                        char ch = str.charAt(i);
                        if (ch == 'a' || ch == 'A') {
                                counters[0]++;
                        } else if (ch == 'e' || ch == 'E') {
                                counters[1]++;
                        } else if (ch == 'i' || ch == 'I') {
                                counters[2]++;
                        } else if (ch == 'o' || ch == 'O') {
                                counters[3]++;
                        } else if (ch == 'u' || ch == 'U') {
                                counters[4]++;
                        }
                }

                // print the frequency of each vowel
                System.out.println("Count of 'a' = " + counters[0]);
                System.out.println("Count of 'e' = " + counters[1]);
                System.out.println("Count of 'i' = " + counters[2]);
                System.out.println("Count of 'o' = " + counters[3]);
                System.out.println("Count of 'u' = " + counters[4]);
        }
}

Output:

Count of 'a' = 1
Count of 'e' = 3
Count of 'i' = 2
Count of 'o' = 1
Count of 'u' = 3

Count vowels using HashMap

We can also use HashMap instead of Array for counters. Let’s look at the program using HashMap.

import java.util.HashMap;
import java.util.Map;

// Java program to demonstrate
// how to count the frequency
// of each vowel using HashMap
public class VowelCounterHashMap {
        public static void main(String[] args) {
                String str = "Java is Excellent Language!!!";

                Map map = new HashMap<>();
                map.put("a", 0);
                map.put("e", 0);
                map.put("i", 0);
                map.put("o", 0);
                map.put("u", 0);

                for (int i = 0; i < str.length(); i++) {
                        char ch = str.charAt(i);
                        if (ch == 'a' || ch == 'A') {
                                map.put("a", map.get("a") + 1);
                        } else if (ch == 'e' || ch == 'E') {
                                map.put("e", map.get("e") + 1);
                        } else if (ch == 'i' || ch == 'I') {
                                map.put("i", map.get("i") + 1);
                        } else if (ch == 'o' || ch == 'O') {
                                map.put("o", map.get("o") + 1);
                        } else if (ch == 'u' || ch == 'U') {
                                map.put("u", map.get("u") + 1);
                        }
                }

                // print the frequency of each vowel
                System.out.println("Count of 'a' = " + map.get("a"));
                System.out.println("Count of 'e' = " + map.get("e"));
                System.out.println("Count of 'i' = " + map.get("i"));
                System.out.println("Count of 'o' = " + map.get("o"));
                System.out.println("Count of 'u' = " + map.get("u"));
        }
}

Output:

Count of 'a' = 4
Count of 'e' = 4
Count of 'i' = 1
Count of 'o' = 0
Count of 'u' = 1
Categories: Java