Problème
Count how many characters in text are uppercase English letters A–Z.
Implement count_uppercase(text).
Input
text: The text to inspect.
Output
Return the number of uppercase letters.
Contraintes
0 ≤ length of text ≤ 10,000 (ASCII characters)
Exemples
Entrée :
text = "Hello World"
Sortie attendue :
expected = 2
Entrée :
std::string text = "Hello World";
Sortie attendue :
long long expected = 2;
Entrée :
string text = "Hello World";
Sortie attendue :
long expected = 2;
Explication
H and W are uppercase: 2.
Entrée :
text = "no caps here!"
Sortie attendue :
expected = 0
Entrée :
std::string text = "no caps here!";
Sortie attendue :
long long expected = 0;
Entrée :
string text = "no caps here!";
Sortie attendue :
long expected = 0;
Explication
There are no uppercase letters: 0.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Scan the text once and notice only letters A through Z count.
Indice 2
Check each character’s case directly instead of comparing against words or splitting the string.
Indice 3
Remember lowercase letters, digits, spaces, and punctuation do not contribute.
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.