Tutte le sfide
Facile 0 risolte 0% di accettazione

Count Uppercase Letters

Count the capital letters in a text.

Cosa imparerai

  • Loop over the characters of a string
  • Detect uppercase letters
  • Count matches

Problema

Mostra traduzione

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.

Vincoli

0 ≤ length of text ≤ 10,000 (ASCII characters)

Esempi

Esempio 1
Input:
text = "Hello World"

Output atteso:
expected = 2
Input:
std::string text = "Hello World";

Output atteso:
long long expected = 2;
Input:
string text = "Hello World";

Output atteso:
long expected = 2;

Spiegazione

H and W are uppercase: 2.

Esempio 2
Input:
text = "no caps here!"

Output atteso:
expected = 0
Input:
std::string text = "no caps here!";

Output atteso:
long long expected = 0;
Input:
string text = "no caps here!";

Output atteso:
long expected = 0;

Spiegazione

There are no uppercase letters: 0.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Scan the text once and notice only letters A through Z count.

Suggerimento 2

Check each character’s case directly instead of comparing against words or splitting the string.

Suggerimento 3

Remember lowercase letters, digits, spaces, and punctuation do not contribute.

Complessità attesa

O(n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.