Created
March 30, 2022 11:48
-
-
Save ricky-lim/5dcebb4efd55b3221fc7baec50e24d63 to your computer and use it in GitHub Desktop.
Count vowels and consonants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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