Write a static method named vowelCount that accepts a String as a parameter and
ID: 657666 • Letter: W
Question
Write a static method named vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the String. The array returned by your method should hold 5 elements: the first is the count of As, the second is the count of Es, the third is the count of Is, the fourth is the count of Os, and the fifth is the count of Us. You may assume that the string contains no uppercase letters.
For example, the call of vowelCount("black banana republic boots") should return an array containing {4, 1, 1, 2, 1}.
Explanation / Answer
public class VowelCountProject {
public static void main(String[] args) {
String data = "bbrb";
int[] result;
result = vowelCount(data);
System.out.print("{");
for (int i = 0; i < result.length; i++) {
if (i == 0) {
System.out.print(result[i]);
} else {
System.out.print(", " + result[i]);
}
}
System.out.println("}");
}
public static int[] vowelCount(String input) {
int[] vowelCountArray = new int[5];
String vowel = "aeiou";
for (int i = 0; i < input.length(); i++) {
int j = vowel.indexOf(input.charAt(i));
if (j < 0) {
} else {
vowelCountArray[j]++;
}
}
return vowelCountArray;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.