Problem overview
This version asks what kinds of characters the password contains: an uppercase letter, a lowercase letter, a digit, a symbol. The program prints the length, one line per kind (Upper, Lower, Digit, Symbol, each True or False), and a verdict. A symbol is any character that is neither a letter nor a digit. The verdict is Strong only when the length is at least 8 and all four kinds are present; otherwise it is Weak.
For Secret!99 every kind is present:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
The previous version tracked a single flag for digits. This one repeats the idea four times in the same loop.
Approach: four flags, one pass
Each kind of character gets its own bool flag, hasUpper, hasLower, hasDigit and hasSymbol, all starting at false. A single loop over the characters tests each character against all four kinds and flips the matching flag to true. After the loop, the flags are printed and the verdict is computed from them.
The four tests are separate if statements, not an else if chain. A character is only one kind, so at most one test will succeed for it, but that does not matter: every test is asked of every character, and the code stays uniform and easy to extend.
Step by step
- Create
lengthfrompassword.length(). - Print
Length:and the number. - Create
hasUpperandhasLowerasbool, bothfalse. - Create
hasDigitandhasSymbolthe same way. - Open a range-based
forwithchar coverpassword. - Test
isupper(c)and sethasUpper. - Test
islower(c)and sethasLower. - Test
isdigit(c)and sethasDigit. - Test
!isalnum(c)and sethasSymbol.isalnumis true for letters and digits, so the!turns it into "neither". - After the loop, print the
UpperandLowerlines, choosingTrueorFalsewith? :. - Print the
DigitandSymbollines in the same way, keeping the order Upper, Lower, Digit, Symbol. - Write the verdict's
if: the length test and the four flags joined with&&, printingStrong. - Add the
elseand printWeak.
The solution in C++
string password = "Secret!99";
int length = password.length();
cout << "Length: " << length << endl;
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSymbol = false;
for (char c : password) {
if (isupper(c)) {
hasUpper = true;
}
if (islower(c)) {
hasLower = true;
}
if (isdigit(c)) {
hasDigit = true;
}
if (!isalnum(c)) {
hasSymbol = true;
}
}
cout << "Upper: " << (hasUpper ? "True" : "False") << endl;
cout << "Lower: " << (hasLower ? "True" : "False") << endl;
cout << "Digit: " << (hasDigit ? "True" : "False") << endl;
cout << "Symbol: " << (hasSymbol ? "True" : "False") << endl;
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
cout << "Strong" << endl;
} else {
cout << "Weak" << endl;
}
Details worth a closer look:
- The classification functions.
isupper,islower,isdigitandisalnumeach take one character and answer a yes-or-no question about it. With ASCII text they behave as their names suggest. - Symbols by elimination. There is no function that asks "is this a symbol" in the lesson's sense, so the code defines it as "not a letter and not a digit". That includes spaces:
!isalnum(' ')is true, so a space counts as a symbol. - The conditional operator and parentheses. Each
(hasUpper ? "True" : "False")is wrapped in parentheses so that the<<chain around it still works.coutwould print the bare flag as 1 or 0. - Five conditions, one
&&chain.length >= 8 && hasUpper && hasLower && hasDigit && hasSymbolis true only when each part is true. One missing kind, or a short password, givesWeak. - Non-ASCII input. The classification functions expect a value that fits in an
unsigned char. A plaincharholding a byte above 127 is usually negative, which makes the call undefined behaviour. Passing(unsigned char)cis the safe form. The lesson's passwords are ASCII.
Watching the flags flip
Printing the four flags after each character of Secret!99 (as 1 for true and 0 for false, in the order upper, lower, digit, symbol) gives:
| Character | Flags after it |
|---|---|
| S | 1000 |
| e | 1100 |
| c, r, e, t | 1100 (no change) |
| ! | 1101 |
| 9 | 1111 |
| 9 | 1111 (no change) |
Flags only ever go from 0 to 1. The capital S sets the first, the first lowercase letter sets the second, the ! sets the symbol flag, and the first 9 completes the set. Characters that repeat a kind change nothing. At the end of the loop the program knows all four facts without having stored the characters themselves.
Complexity
Time: O(n) for n characters: each one gets four constant-time tests. Space: O(1): four flags and a length.
Edge cases and common mistakes
- Length is not enough.
Ab1!contains all four kinds, but it has 4 characters, so the verdict isWeak. - One kind missing.
Secret99has 8 characters with upper, lower and digit, but no symbol, so it isWeak. - Using
else ifbetween the four tests. It happens to give the same flags here, because a character is only one kind. It also stops the code from working the day a character should count for two things, which is why the lesson uses separateifstatements. - Forgetting the
!in the symbol test.isalnum(c)alone would flag letters and digits as symbols, and every normal password would showSymbol: True. - Printing flags in a different order. The output must list Upper, Lower, Digit, Symbol.
- Treating a space as a letter. It is neither a letter nor a digit, so it counts as a symbol.
Testing
| password | Upper | Lower | Digit | Symbol | Verdict |
|---|---|---|---|---|---|
| Secret!99 | True | True | True | True | Strong (Length: 9) |
| secret99 | False | True | True | False | Weak (Length: 8) |
| Secret99 | True | True | True | False | Weak (Length: 8) |
| Ab1! | True | True | True | True | Weak (Length: 4) |
The next version builds on this program.
