Implement the method countVowels
that returns the number of vowels present in a given string.
Learner Level
String Manipulation Loop
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.
To count the number of vowels in a string, follow these steps:
-
Initialize a Counter
Start with a variable set to 0 to track the vowel count. -
Iterate Through the String
Loop over each character in the input string. -
Check for Vowels
For each character, check whether it is a vowel (a, e, i, o, u), considering both lowercase and uppercase. -
Update the Count
If it is a vowel, increment the counter. -
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