Implement the method countConsonants that returns the number of consonants in a string.
Count consonant letters in a string. Vowels are a, e, i, o, and u, in either lowercase or uppercase form.
The task is designed to test careful handling of edge cases, not only the most common input.
- Ignore digits, spaces, and punctuation.
- Count both uppercase and lowercase consonants.
- Return
0when no consonant exists.
Example 1
Input
s (string) =
helloReturns (int)
3Example 2
Input
s (string) =
AEIOUReturns (int)
0Example 3
Input
s (string) =
W3SchoolsReturns (int)
6Example 4
Input
s (string) =
skyReturns (int)
3For each character, first check whether it is alphabetic. If it is a letter and not a vowel, increase the count.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run