Problem overview
The checker gets a second rule. A password is now Strong only if it has at least 8 characters and contains at least one digit; otherwise it is Weak. The program prints three lines: the length, whether a digit was found (written as True or False), and the verdict.
For Secret99 the output is:
Length: 8
Has digit: True
Strong
For abcdefgh the length passes but there is no digit:
Length: 8
Has digit: False
Weak
The previous version only compared the length. This one has to look inside the string.
Approach: a flag that flips once
To find out whether any character is a digit, the program makes a bool flag, hasDigit, that starts as false: no digit seen yet. It then goes through the password one character at a time, and the first time a character is a digit it sets the flag to true. Nothing ever sets it back to false, so once a digit has been seen the flag stays true, no matter what characters follow.
When the loop ends, the flag answers the question for the whole password, and the verdict combines it with the length test using &&.
Step by step
- Create
lengthfrompassword.length(). It is used in the first output line and in the verdict. - Print
Length:followed by the number. - Create
bool hasDigit = false;, the "not seen yet" state. - Write a range-based
forwhere thechar cgoes overpassword. It hands you one character per pass. - Inside the loop, test
isdigit(c)and sethasDigittotruewhen it holds. - After the loop, print
Has digit:and the flag, as the textTrueorFalse. - Write the verdict's
ifwithlength >= 8 && hasDigitand printStrong. - Add the
elseand printWeak.
The solution in C++
string password = "Secret99";
int length = password.length();
cout << "Length: " << length << endl;
bool hasDigit = false;
for (char c : password) {
if (isdigit(c)) {
hasDigit = true;
}
}
cout << "Has digit: " << (hasDigit ? "True" : "False") << endl;
if (length >= 8 && hasDigit) {
cout << "Strong" << endl;
} else {
cout << "Weak" << endl;
}
Details worth a closer look:
- Range-based
for.for (char c : password)means "for each charactercinpassword". It gives you a copy of each character, in order, with no index to manage. isdigit. It is true for the characters'0'to'9'and false for everything else, including letters, spaces and symbols. It comes from the C character functions and takes one character.- A
boolprints as a number.cout << trueprints1andcout << falseprints0. The lesson needs the wordsTrueandFalse, so it picks the text itself with the conditional operator:hasDigit ? "True" : "False"is the first text when the flag is true and the second when it is false. - Parentheses around the conditional. The expression sits inside
( ... )in thecoutchain because<<binds more tightly than? :. Without them the compiler groupscout << "Has digit: " << hasDigitas the condition and tries to apply<<to"False"andendl, which fails with an error about invalid operands tooperator<<. &&needs both sides. The verdict isStrongonly when the length test and the flag are both true. Either one failing, or both, givesWeak.- The loop doesn't stop early. It checks every character even after a digit was found. For a password this costs next to nothing, and it keeps the code simple: there is no
breakto reason about.
Why one flag is enough
The flag stores a single fact about everything seen so far: has a digit appeared yet? That is all the verdict needs. Which character was the digit, or how many digits there were, does not change the answer, so the program doesn't have to remember which character was the digit or where it was. Following Secret99, the flag stays false for the letters S, e, c, r, e, t, becomes true at the first 9 and remains true at the second. For abcdefgh it never changes.
Complexity
Time: O(n) for a password of n characters, since each character is examined once. Space: O(1): the program stores a length and one flag, whatever the password size.
Edge cases and common mistakes
- Digits only.
12345678has 8 characters and a digit, so it isStrongunder this version's two rules. Letters and symbols are not checked yet. - Too short but with a digit.
a1printsLength: 2,Has digit: TrueandWeak: the digit alone is not enough. - Setting the flag back to false. An
elsebranch that resetshasDigiton every non-digit would make the flag reflect only the last character. - Printing the flag directly.
cout << hasDigitprints1or0, notTrueorFalse. - Using
||in the verdict. It would accept a short password that has a digit, or a long one without any. - Non-ASCII input.
isdigitexpects a value that fits in anunsigned char. A plaincharholding a byte above 127 is usually negative, and passing it is undefined behaviour. Casting with(unsigned char)cavoids that; the lesson's passwords are ASCII.
Testing
| password | Output |
|---|---|
| Secret99 | Length: 8, Has digit: True, Strong |
| abcdefgh | Length: 8, Has digit: False, Weak |
| 12345678 | Length: 8, Has digit: True, Strong |
| a1 | Length: 2, Has digit: True, Weak |
The next version builds on this program.
