Problem overview
The last version of the project combines the earlier checks into one number. The program starts a score at 0 and adds points for each rule the password meets:
- 25 points for a length of at least 8;
- 20 points for an uppercase letter;
- 20 points for a lowercase letter;
- 20 points for a digit;
- 15 points for a symbol, meaning a character that is neither a letter nor a digit.
It then prints the length and the score. For Secret!99 every rule passes, and 25 + 20 + 20 + 20 + 15 is 100:
Length: 9
Score: 100
For abc123 only the lowercase letters and the digits count, so the output is Length: 6 and Score: 40.
Approach: collect the facts, then score them
The program has two phases. First it gathers facts about the password, with the same technique as the earlier versions: a loop over the characters that flips four bool flags. Then it turns the facts into points, with one if per rule that adds to score.
Keeping the phases apart makes each one easy to read. The loop answers "what is in the password?" and the scoring answers "what is that worth?". Changing a rule's weight touches one line in the second phase and nothing in the first.
Step by step
- Create
lengthfrompassword.length(). It is used for the length rule and for the first output line. - Create
hasUpper,hasLower,hasDigitandhasSymbolasboolflags, eachfalse: "not seen yet". - Open a range-based
forwithchar coverpassword. - Inside it, test
isupper(c),islower(c),isdigit(c)and!isalnum(c), each in its ownif, and set the matching flag. - Create
int score = 0;after the loop. - Add the length rule:
if (length >= 8)thenscore += 25. - Add the three 20-point rules for
hasUpper,hasLowerandhasDigit. - Add the 15-point rule for
hasSymbol. - Print
Length:and the length, then printScore:and the score, each withendl.
The solution in C++
string password = "Secret!99";
int length = password.length();
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;
}
}
int score = 0;
if (length >= 8) {
score += 25;
}
if (hasUpper) {
score += 20;
}
if (hasLower) {
score += 20;
}
if (hasDigit) {
score += 20;
}
if (hasSymbol) {
score += 15;
}
cout << "Length: " << length << endl;
cout << "Score: " << score << endl;
Details worth a closer look:
- Every rule is its own
if. Anelse ifchain would stop after the first rule that matched, so a password could never earn points from more than one rule. Independentifstatements let all five rules contribute. score += 25is shorthand forscore = score + 25.- The weights add up to 100. 25 + 20 + 20 + 20 + 15 is 100, which is why the score is always between 0 and 100 and no clamping is needed.
- Flags are read like conditions.
if (hasUpper)needs no== true: the variable is already abool. - Symbols by elimination.
!isalnum(c)means "not a letter and not a digit", so a space counts as a symbol. - Non-ASCII input. The character 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.
Adding up the points
The score is a sum of independent contributions, so it helps to see which rule paid what. For Secret!99 (9 characters), the length rule gives 25, the capital S gives 20, the lowercase letters give 20, the 9s give 20 and the ! gives 15, for a total of 100. For abc123 (6 characters) the length rule fails, there is no capital letter and no symbol, and the lowercase letters and digits give 20 + 20 = 40.
Why the score is not computed inside the loop
It is tempting to add points as soon as a character matches, but that would pay the same rule once per matching character: a password with five lowercase letters would earn 100 points for them. The flags prevent this. A flag is either set or not, however many characters set it, so each rule is paid at most once. That is also why the scoring comes after the loop: the points are decided only when the whole password has been seen, and the loop's only job is to record which classes appeared.
Complexity
Time: O(n) for a password of n characters: one pass with four tests per character, and five comparisons afterwards. Space: O(1): a length, four flags and a score.
Edge cases and common mistakes
- An empty password. No flag is set and the length rule fails, so the output is
Length: 0andScore: 0. - Long but one-dimensional.
ABCDEFGHandabcdefghboth have 8 characters and one letter class, so each scores 25 + 20 = 45. - Short but varied. A password can earn points from the classes while failing the length rule, as
abc123does. - Using
else ifbetween the scoring rules. It would add at most one amount to the score. - Counting characters instead of classes. The score rewards having a class, not having many characters of it, so a flag is the right tool and a counter is not needed.
- Printing the score before the length. The output lists the length line first.
Testing
| password | Output |
|---|---|
| Secret!99 | Length: 9, Score: 100 |
| abc123 | Length: 6, Score: 40 |
| (empty string) | Length: 0, Score: 0 |
| ABCDEFGH | Length: 8, Score: 45 |
| abcdefgh | Length: 8, Score: 45 |
