Все задачи
Легко Решено: 0 Процент решений: 0%

Count Uppercase Letters

Count the capital letters in a text.

Чему вы научитесь

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

Условие

Показать перевод

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.

Ограничения

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

Примеры

Пример 1
Ввод:
text = "Hello World"

Ожидаемый вывод:
expected = 2
Ввод:
std::string text = "Hello World";

Ожидаемый вывод:
long long expected = 2;
Ввод:
string text = "Hello World";

Ожидаемый вывод:
long expected = 2;

Пояснение

H and W are uppercase: 2.

Пример 2
Ввод:
text = "no caps here!"

Ожидаемый вывод:
expected = 0
Ввод:
std::string text = "no caps here!";

Ожидаемый вывод:
long long expected = 0;
Ввод:
string text = "no caps here!";

Ожидаемый вывод:
long expected = 0;

Пояснение

There are no uppercase letters: 0.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

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

Подсказка 2

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

Подсказка 3

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

Ожидаемая сложность

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

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.