Question:
How to count vowels in a string?
How to count upper case vowels in a string?
How to count lower case vowels in a string?
How to count upper case vowels in a string?
How to count lower case vowels in a string?
Code:
public class CountVowels { public static void main(String[] args) { String str = "abcdefg HIJKLMNOP qrstuvwxyz"; int upper = 0; int lower = 0; int non = 0; String vowels = "aeiouAEIOU"; char[] array = str.toCharArray(); for(char c: array) { if(vowels.indexOf(c) != -1){ if(Character.isLowerCase(c)){ lower = lower + 1; } else if(Character.isUpperCase(c)){ upper = upper + 1; } } else { non = non + 1; } } System.out.println("String: " + str); System.out.println("Upper case vowels: " + upper); System.out.println("Lower case vowels: " + lower); System.out.println("Non vowels: " + non); } }
Output:
$ java CountVowels String: abcdefg HIJKLMNOP qrstuvwxyz Upper case vowels: 2 Lower case vowels: 3 Non vowels: 23