Problema
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.
Vincoli
0 ≤ length ≤ 10,000
Esempi
Input:
text = "Hello World"
Output atteso:
expected = 3
Input:
std::string text = "Hello World";
Output atteso:
long long expected = 3;
Input:
string text = "Hello World";
Output atteso:
long expected = 3;
Spiegazione
e, o and o are vowels: 3.
Input:
text = "AEIOU and y"
Output atteso:
expected = 6
Input:
std::string text = "AEIOU and y";
Output atteso:
long long expected = 6;
Input:
string text = "AEIOU and y";
Output atteso:
long expected = 6;
Spiegazione
A, E, I, O, U, a are vowels; y is not counted: 6.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Scan the text once and test each character against the vowel set.
Suggerimento 2
Treat uppercase and lowercase vowels as the same kind of letter.
Suggerimento 3
An empty string should simply return zero.
Complessità attesa
O(n) time, O(1) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.