DSA FUNDAMENTALS › STRING BASICS
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.
int letters = 0;
int digits = 0;
int special = 0;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.
if ((s[i] >= 'A' && s[i] <= 'Z') ||
(s[i] >= 'a' && s[i] <= 'z')) {
letters++;
}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.
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.
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.
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.
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.
| INDEX | CHARACTER | CATEGORY | LETTERS | DIGITS | SPECIAL |
|---|---|---|---|---|---|
| 0 | 'A' | letter | 1 | 0 | 0 |
| 1 | 'b' | letter | 2 | 0 | 0 |
| 2 | '7' | digit | 2 | 1 | 0 |
| 3 | '#' | special | 2 | 1 | 1 |
| 4 | space | special | 2 | 1 | 2 |
| 5 | '2' | digit | 2 | 2 | 2 |
| 6 | '!' | special | 2 | 2 | 3 |
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).