Problème
Count how many characters in text are vowels: a, e, i, o, u in either uppercase or lowercase.
Implement count_vowels(text).
Input
text: The text to inspect (ASCII characters).
Output
Return the number of vowels.
Contraintes
0 ≤ length ≤ 10,000
Exemples
Entrée :
text = "Hello World"
Sortie attendue :
expected = 3
Entrée :
std::string text = "Hello World";
Sortie attendue :
long long expected = 3;
Entrée :
string text = "Hello World";
Sortie attendue :
long expected = 3;
Explication
e, o and o are vowels: 3.
Entrée :
text = "AEIOU and y"
Sortie attendue :
expected = 6
Entrée :
std::string text = "AEIOU and y";
Sortie attendue :
long long expected = 6;
Entrée :
string text = "AEIOU and y";
Sortie attendue :
long expected = 6;
Explication
A, E, I, O, U, a are vowels; y is not counted: 6.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Scan the text once and test each character against the vowel set.
Indice 2
Treat uppercase and lowercase vowels as the same kind of letter.
Indice 3
An empty string should simply return zero.
Complexité attendue
O(n) time, O(1) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.