Problem overview
Instead of a yes or no verdict, this version gives the password a numeric score from 0 to 100. The score starts at 0 and gains points for each rule the password meets:
- 25 points if the length is 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.
The program prints the length and the score. For "Secret!99" every rule is met, and 25 + 20 + 20 + 20 + 15 is 100:
Length: 9
Score: 100
For "abc123" the length is 6, so the length rule fails. There is a lowercase letter and a digit, and nothing else, so the score is 20 + 20 = 40.
Approach: find the facts first, then add up the points
The solution has two stages. The first stage is the one from the flag lessons: a loop over the characters sets four bool flags, one for each character class. The second stage turns facts into points: a series of if statements, each adding its amount to score when its condition holds.
Keeping the stages apart matters. Each class is worth its points once, however many matching characters there are. If the points were added inside the loop, every uppercase letter would add 20 again. With "Secret!99" that mistake gives a score of 200, not 100. The flags answer "does the password have this class?", which is exactly the question the scoring rules ask.
Each rule is its own if, never an else if. The rules are independent and several of them usually apply at once, so the chain would stop adding after the first match.
Step by step
- Store the length in
length. It is used for the length rule and for the first output line. - Create the four flags,
hasUpper,hasLower,hasDigitandhasSymbol, each aboolthat starts atfalse. - Loop over the characters. A
foreach (char c in password)runs the body once per character, 9 times for this password. - Set the flags. Four
ifstatements usechar.IsUpper,char.IsLower,char.IsDigitand!char.IsLetterOrDigit. A flag only ever changes totrue. - Create the score.
int score = 0;goes after the loop. The points are added to it next. - Award the points.
length >= 8adds 25, thenhasUpper,hasLowerandhasDigitadd 20 each, andhasSymboladds 15. - Print the two lines once the score is final:
Length:with the length, thenScore:with the score.
The solution in C#
string password = "Secret!99";
int length = password.Length;
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSymbol = false;
foreach (char c in password) {
if (char.IsUpper(c)) {
hasUpper = true;
}
if (char.IsLower(c)) {
hasLower = true;
}
if (char.IsDigit(c)) {
hasDigit = true;
}
if (!char.IsLetterOrDigit(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;
}
Console.WriteLine("Length: " + length);
Console.WriteLine("Score: " + score);
The C# details worth a closer look:
+=adds and assigns.score += 25meansscore = score + 25. It changes the variable in place.- A
boolworks as a condition on its own.if (hasUpper)needs no comparison, and writingif (hasUpper == true)says the same thing in more words. - Declaration order. C# requires a variable to be declared before it is used, which is why
scoreappears after the flags it depends on. Declaring it a few lines earlier would work equally well, as long as it starts at 0. - The
intnever overflows here. The score is at most 100, and anintholds values up to about 2.1 billion.
Why 25, 20, 20, 20 and 15
The weights sum to 100: 25 + 20 + 20 + 20 + 15. A password that meets every rule scores exactly 100, and one that meets none scores 0. The length rule carries the most weight, and the symbol rule the least. The numbers are the lesson's choice, and changing one would change the maximum.
Edge cases and common mistakes
- Short but varied.
"Aa1!"has all four classes but only 4 characters, so it scores 20 + 20 + 20 + 15 = 75. The length points are separate. - Long but plain.
"12345678"gets 25 for the length and 20 for the digits: 45. - Short and plain.
"a!"gets 20 for the lowercase letter and 15 for the symbol, a score of 35. - Empty password.
""never enters the loop, no rule fires, and the output isLength: 0andScore: 0. - Adding points inside the loop. It counts characters instead of classes, as shown above, and the score can go far past 100.
- Using
else iffor the rules. Once one rule matches, the others would be skipped, and the score would never be more than the first matching rule's points. - Printing before scoring. The two output lines come after all the points have been added. Printed earlier, the score would show a partial total.
Testing
"Secret!99"givesLength: 9andScore: 100."abc123"givesLength: 6andScore: 40."ABCDEFGH"givesLength: 8andScore: 45."Aa1!"givesLength: 4andScore: 75.""givesLength: 0andScore: 0.
