Skip to content

Instantly share code, notes, and snippets.

@ricky-lim
Created March 30, 2022 11:48
Show Gist options
  • Save ricky-lim/5dcebb4efd55b3221fc7baec50e24d63 to your computer and use it in GitHub Desktop.
Save ricky-lim/5dcebb4efd55b3221fc7baec50e24d63 to your computer and use it in GitHub Desktop.
Count vowels and consonants
import java.util.List;
import java.util.stream.Collectors;
class Scratch {
public static void countVowelAndConsonant(String s) {
String VOWELS = "aeiouy";
String normalized = s.toLowerCase().trim();
List<Integer> letters = normalized.chars()
.filter(Character::isAlphabetic)
.boxed()
.collect(Collectors.toList());
long vowelCount = letters.stream()
.filter(c -> VOWELS.indexOf(c) != -1)
.count();
long consonantCount = (long) letters.size() - vowelCount;
System.out.println(vowelCount + " vowels" + " in " + s);
System.out.println(consonantCount + " consonants" + " in " + s);
System.out.println();
}
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "There is a quiet Mouse";
String s3 = "I am Happy";
countVowelAndConsonant(s1);
countVowelAndConsonant(s2);
countVowelAndConsonant(s3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment