import java.util.Scanner;
public class VowelFrequencyCounter {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print(“Enter a string: “);
String input = scanner.nextLine();
// Call the method to count vowels
int[] vowelCounts = countVowels(input);
// Print the frequencies of each vowel
System.out.println(“Vowel Frequencies:”);
char[] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};
for (int i = 0; i < vowels.length; i++) {
System.out.println(vowels[i] + “: ” + vowelCounts[i]);
}
// Close the scanner
scanner.close();
}
// Method to count frequencies of each vowel in the input string
public static int[] countVowels(String str) {
// Convert the string to lower case for case-insensitive comparison
str = str.toLowerCase();
// Array to store frequency of each vowel
int[] vowelCount = new int[5];
// Iterate through the string and count vowels
for (char ch : str.toCharArray()) {
switch (ch) {
case ‘a’:
vowelCount[0]++;
break;
case ‘e’:
vowelCount[1]++;
break;
case ‘i’:
vowelCount[2]++;
break;
case ‘o’:
vowelCount[3]++;
break;
case ‘u’:
vowelCount[4]++;
break;
default:
// Ignore non-vowel characters
break;
}
}
return vowelCount;
}
}
Leave a Reply