Problem overview
The previous version only asked whether each kind of character was present. This one asks how many of each there are. The program prints four lines: the number of uppercase letters, lowercase letters, digits and symbols in the password, where a symbol is any character that is neither a letter nor a digit.
For Hello!World9 the output is:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
There is no verdict line in this version: the task is the counting itself.
Approach: swap each flag for a counter
A flag remembers "seen or not". A counter remembers "how many times". The loop has the same shape as before, one pass over the characters with one test per class, but each test now adds 1 to an int that started at 0 instead of setting a bool to true.
The four counters are independent, and each character matches exactly one of the four tests, so after the pass the four numbers add up to the length of the password.
Step by step
- Create
uppercaseas anintset to 0. - Create
lowercasethe same way. - Create
digitsthe same way. - Create
symbolsthe same way. A space counts as a symbol, because it is neither a letter nor a digit. - Open a range-based
forwithchar coverpassword. - If
isupper(c), douppercase++. - If
islower(c), dolowercase++. - If
isdigit(c), dodigits++. - If
!isalnum(c), dosymbols++. - After the loop, print the
Uppercaseline: the label, the counter andendl.coutprints anintas digits. - Print the
Lowercase,DigitsandSymbolslines the same way, in that order.
The solution in C++
string password = "Hello!World9";
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int symbols = 0;
for (char c : password) {
if (isupper(c)) {
uppercase++;
}
if (islower(c)) {
lowercase++;
}
if (isdigit(c)) {
digits++;
}
if (!isalnum(c)) {
symbols++;
}
}
cout << "Uppercase: " << uppercase << endl;
cout << "Lowercase: " << lowercase << endl;
cout << "Digits: " << digits << endl;
cout << "Symbols: " << symbols << endl;
Details worth a closer look:
- Counters start at zero. A local
intthat is declared without a value holds an unspecified number. The= 0is what makes the counts correct. x++means add 1. It is shorthand forx = x + 1. Used as a statement on its own, as here, it only increases the variable.- The printing is outside the loop. The four lines run once, after every character has been counted. Placed inside the loop, they would print the running counts twelve times.
- Four
ifs, not anelse ifchain. A character falls into at most one class, so a chain would give the same counts here. Separateifstatements keep each rule readable on its own. - Symbols by elimination.
!isalnum(c)is true for anything that is not a letter or a digit, including punctuation and spaces. - Non-ASCII input.
isupperand the other functions expect a value that fits in anunsigned char. A plaincharholding a byte above 127 is usually negative, which makes the call undefined behaviour; the safe form isisupper((unsigned char)c). The lesson's passwords are ASCII.
Following Hello!World9
The password has twelve characters. The H and the W bump uppercase, the letters ello and orld bump lowercase eight times, the ! bumps symbols and the final 9 bumps digits. That gives 2, 8, 1 and 1, and the four numbers add up to 12, the password's length. If the sum is ever different from the length, a character was missed or counted twice.
Counters contain the flags
A counter carries more information than a flag. The flag hasDigit from the earlier versions is the same fact as the condition digits > 0, so a program with these four counters can still answer every yes-or-no question from before. The reverse is not true: a flag cannot be turned back into a count. That is why counting is the more general tool, and why it costs nothing extra here: the loop is the same length and does the same number of tests. The only difference is the statement in each branch: digits++ instead of hasDigit = true.
Complexity
Time: O(n) for n characters, each tested a constant number of times. Space: O(1): four integers, however long the password is.
Edge cases and common mistakes
- Spaces.
Pass word!2has the space and the!, soSymbolsis 2, not 1. The output isUppercase: 1,Lowercase: 7,Digits: 1,Symbols: 2. - An empty password. The loop never runs, and all four lines print 0.
- A missing initialiser. Without
= 0a counter starts from an unspecified value and the output is wrong or varies between runs. - Counting with a flag. Setting a
boolinstead of incrementing would say whether a class exists, but not how many characters belong to it. - Printing inside the loop. It repeats the lines once per character.
- Wrong line order. The output is Uppercase, Lowercase, Digits, Symbols.
Testing
| password | Uppercase | Lowercase | Digits | Symbols |
|---|---|---|---|---|
| Hello!World9 | 2 | 8 | 1 | 1 |
| Pass word!2 | 1 | 7 | 1 | 2 |
| (empty string) | 0 | 0 | 0 | 0 |
The next version builds on this program.
