# Java Program to Count the Frequency of Each Vowel in a Given String

Write a program to count the frequency of each vowel in a given string in Java.

For example,

```Input
"My name is John Smith"
Output
Number of 'a' = 1
Number of 'e' = 1
Number of 'i' = 2
Number of 'o' = 1
Number of 'u' = 0```

Steps

1. Let str be the input string.
2. Initialize an integer array of size 5. Let the name of the array be cnt.
3. Fill cnt with zeros.
4. cnt stores the frequency of ‘a’, cnt stores the frequency of ‘e’ and so on ( in order a,e,i,o,u ).
5. Traverse each character of str using a loop
1. If the character is ‘a’, increment cnt.
2. If the character is ‘e’, increment cnt.
3. If the character is ‘i’, increment cnt.
4. If the character is ‘o’, increment cnt.
5. If the character is ‘u’, increment cnt.
6. Print the frequency of each vowel.
``````import java.util.Scanner;

public class Main
{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String str;
// array is always initialized with zeros
int cnt[] = new int;

System.out.print("Enter String: ");
str = sc.nextLine();

for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'A') {
cnt++;
}
if (str.charAt(i) == 'e' || str.charAt(i) == 'E') {
cnt++;
}
if (str.charAt(i) == 'i' || str.charAt(i) == 'I') {
cnt++;
}
if (str.charAt(i) == 'o' || str.charAt(i) == 'O') {
cnt++;
}
if (str.charAt(i) == 'u' || str.charAt(i) == 'U') {
cnt++;
}
}

System.out.println("Number of 'a' = " + cnt);
System.out.println("Number of 'e' = " + cnt);
System.out.println("Number of 'i' = " + cnt);
System.out.println("Number of 'o' = " + cnt);
System.out.println("Number of 'u' = " + cnt);
}
}``````

Output

``````Enter String: Hello, My name is Peter Pan
Number of 'a' = 2
Number of 'e' = 4
Number of 'i' = 1
Number of 'o' = 1
Number of 'u' = 0``````