DSA SheetLesson · no judge

DSA FUNDAMENTALSSTRING BASICS

Count Letters, Digits, and Special Characters in One Pass

Reading · 5 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 13

Every character belongs to exactly one of the three counters

To count letters, digits, and special characters, keep three counters and start each at 0. A letter is an uppercase or lowercase alphabetic character. A digit is a character from '0' through '9'. A special character is anything that is neither a letter nor a digit. For s = "Ab7# 2!", every character at an index must increase exactly one counter, so the final counts describe all 7 characters without overlap or omission.

CPPAll three counters start at zero.
int letters = 0;
int digits = 0;
int special = 0;
  • letters counts alphabetic characters
  • digits counts characters from '0' to '9'
  • special counts every remaining character, including spaces

A letter test needs both uppercase and lowercase ranges

ASCII places uppercase letters in the range 'A' through 'Z' and lowercase letters in the range 'a' through 'z'. The character at index 0 is 'A', so it matches the uppercase range. The character at index 1 is 'b', so it matches the lowercase range. The two range tests must be joined with OR, because a character only needs to belong to one of the two letter ranges.

CPPThe OR accepts either uppercase or lowercase letters.
if ((s[i] >= 'A' && s[i] <= 'Z') ||
    (s[i] >= 'a' && s[i] <= 'z')) {
    letters++;
}
CHECKPOINT 1Not answered

Which condition recognizes both 'A' at index 0 and 'b' at index 1 as letters?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The final else counts the space as a special character

After testing the letter condition, test whether the character is a digit. Every character that fails both tests belongs in the final else branch. That branch receives '#', the space at index 4, and '!'. The space is not punctuation, but it is still special because it is neither a letter nor a digit. The chain also guarantees that one character cannot increase two counters.

CPPThe final else catches every character that is neither a letter nor a digit.
if ((s[i] >= 'A' && s[i] <= 'Z') ||
    (s[i] >= 'a' && s[i] <= 'z')) {
    letters++;
} else if (s[i] >= '0' && s[i] <= '9') {
    digits++;
} else {
    special++;
}

At index 3, '#' fails the letter test and the digit test, so special increases. At index 4, the character is a space, and it fails both tests in exactly the same way. At index 6, '!' also reaches special. These three characters are different, but the definition of special treats all three alike.

CHECKPOINT 2Not answered

Enter the final counts for "Ab7# 2!" in this order: letters, digits, special characters.

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

the seven indexed slots of "Ab7# 2!" classified into three countersSeven slots show index 0 containing 'A', index 1 containing 'b', index 2 containing '7', index 3 containing '#', index 4 containing a space, index 5 containing '2', and index 6 containing '!'. Arrows send 'A' and 'b' to the letter counter with value 2, '7' and '2' to the digit counter with value 2, and '#', the space, and '!' to the special counter with value 3. Every slot has exactly one arrow.0123456Ab7#[ ]2!letters = 2digits = 2special = 3Each character maps to exactly one counterThe marked space at index 4 is counted as special.
The space is one of the three characters counted as special.

One pass produces the totals 2 letters, 2 digits, and 3 special characters

Scan s from index 0 through index 6 and apply the same chain at each position. 'A' and 'b' raise the letter counter, '7' and '2' raise the digit counter, and '#', the space, and '!' raise the special counter. After the last character, the counters are letters = 2, digits = 2, and special = 3.

INDEXCHARACTERCATEGORYLETTERSDIGITSSPECIAL
0'A'letter100
1'b'letter200
2'7'digit210
3'#'special211
4spacespecial212
5'2'digit222
6'!'special223
The counters after processing each character.

If the string has length n, the loop visits each character once, so its time complexity is O(n). The three counters and the loop index use a fixed amount of extra memory, regardless of the string length, so the extra space complexity is O(1).