Learner Level

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 0 when no consonant exists.

Example 1

Input
s (string) = hello
Returns (int) 3

Example 2

Input
s (string) = AEIOU
Returns (int) 0

Example 3

Input
s (string) = W3Schools
Returns (int) 6

Example 4

Input
s (string) = sky
Returns (int) 3

For 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.