Learner Level

Implement the method countVowels that returns the number of vowels present in a given string.

Write a function that takes a string as input and counts how many vowels it contains. The vowels to be counted are: a, e, i, o, u — both uppercase and lowercase should be considered.

Example 1

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

Example 2

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

Example 3

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

Example 4

Input
s (string) = This is a test
Returns (int) 4

To count the number of vowels in a string, follow these steps:

  1. Initialize a Counter
    Start with a variable set to 0 to track the vowel count.

  2. Iterate Through the String
    Loop over each character in the input string.

  3. Check for Vowels
    For each character, check whether it is a vowel (a, e, i, o, u), considering both lowercase and uppercase.

  4. Update the Count
    If it is a vowel, increment the counter.

  5. Return the Final Count
    After the loop finishes, return the count as the result.

Pseudocode:

Function countVowels(s):
    Initialize count ← 0
    For each character ch in s:
        Convert ch to lowercase
        If ch is 'a' or 'e' or 'i' or 'o' or 'u':
            Increment count by 1
    Return 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.