Blogger

Delete comment from: Java67

JohnDoe said...

public static Map countVowelsAndConsonants(String input){
Map result = new HashMap<>();
int vowelsCounter = 0;
int consonantsCounter =0;
Map vowels = new HashMap(){{
put('a', 'a');
put('e', 'e');
put('i', 'i');
put('o', 'o');
put('u', 'u');
}};

for(Character c : input.toCharArray()){
int unicode = (int) c;
if(unicode >= 97 && unicode <= 122 || unicode >= 65 && unicode <= 90){
if(vowels.get(c) == null){
consonantsCounter++;
}else{
vowelsCounter++;
}
}
}

result.put("vowels", vowelsCounter);
result.put("consonants", consonantsCounter);
return result;
}

Sep 15, 2019, 1:26:55 PM


Posted to [Solved] How to count Vowels and Consonants in Java String Word? Example

Google apps
Main menu