All challenges
Easy 0 solved 0% acceptance

Count Uppercase Letters

Count the capital letters in a text.

What you'll learn

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

Problem

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.

Constraints

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

Examples

Example 1
Input:
text = "Hello World"

Expected output:
expected = 2
Input:
std::string text = "Hello World";

Expected output:
long long expected = 2;
Input:
string text = "Hello World";

Expected output:
long expected = 2;

Explanation

H and W are uppercase: 2.

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

Expected output:
expected = 0
Input:
std::string text = "no caps here!";

Expected output:
long long expected = 0;
Input:
string text = "no caps here!";

Expected output:
long expected = 0;

Explanation

There are no uppercase letters: 0.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

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

Hint 2

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

Hint 3

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

Expected complexity

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

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.