Java program to check occurrence of each vowel in String


To count occurence of vowels in a string again use Map utility of java as used in calculating occurence of each character in string.Put each vowel as key in Map and put initial value as 1 for each key.Now compare each character with key of map if a character matches with a key then increase its corresponding value by 1.

Example

public class OccurenceVowel {
   public static void main(String[] args) {
      String str = "AEAIOG";
      LinkedHashMap<Character, Integer> hMap = new LinkedHashMap();
      hMap.put('A', 0);
      hMap.put('E', 0);
      hMap.put('I', 0);
      hMap.put('O', 0);
      hMap.put('U', 0);
      for (int i = 0; i <= str.length() - 1; i++) {
         if (hMap.containsKey(str.charAt(i))) {
            int count = hMap.get(str.charAt(i));
            hMap.put(str.charAt(i), ++count);
         }      
      }
      System.out.println(hMap);
   }
}

Output

{A=2, E=1, I=1, O=1, U=0}

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements