Problem overview
This version combines the earlier checks into one number. A password earns points for each property it has: 25 for a length of at least 8, 20 for an uppercase letter, 20 for a lowercase letter, 20 for a digit and 15 for a symbol (a character that is neither a letter nor a digit). The maximum is 25 + 20 + 20 + 20 + 15 = 100.
The program prints the length and then the score. For Secret!99, which has every property:
Length: 9
Score: 100
For abc123, which has lowercase letters and digits but is only 6 characters long:
Length: 6
Score: 40
Approach: collect the facts, then add the points
The solution has two phases. First, one pass over the characters sets four boolean flags, exactly as in the character-class version: hasUpper, hasLower, hasDigit and hasSymbol. Second, a series of if statements turns the facts into points.
Keeping the phases apart makes the scoring easy to read and to change. The loop only answers "what is in this password?", and the scoring only answers "what is that worth?" Changing a weight touches one line, and the loop stays as it is.
Every scoring rule is its own if, never an else if. The rules are independent: a password can earn the length points and the digit points together. With an else if chain, only the first matching rule would ever add anything.
Step by step
- Store the length in
length, frompassword.length(). It is used for the score and for the first output line. - Create the four flags,
hasUpper,hasLower,hasDigitandhasSymbol, each abooleanset tofalse, meaning "not seen yet". - Loop over
password.toCharArray(), with thecharvariablec. - Set the flags. Four
ifstatements in the loop testCharacter.isUpperCase(c),Character.isLowerCase(c),Character.isDigit(c)and!Character.isLetterOrDigit(c), and set the matching flag totrue. A flag only flips one way, so nothing sets it back. - Create
scoreat 0 after the loop. It collects the points. - Add the length points. When
length >= 8,score += 25. - Add the class points. One
iffor each flag: 20 forhasUpper, 20 forhasLower, 20 forhasDigitand 15 forhasSymbol. - Print the result. The
Length:line comes first, thenScore:.
How the score adds up
For abc123 the flags end as hasLower and hasDigit true, the other two false, and the length is 6, below 8. Only two rules fire, 20 for the lowercase letters and 20 for the digits, and the score is 40. For Secret!99 all five rules fire: 25 + 20 + 20 + 20 + 15 is 100. Each rule is one question about the password, and the score is the sum of the answers that were yes.
The solution in Java
String password = "Secret!99";
int length = password.length();
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
boolean hasSymbol = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUpper = true;
}
if (Character.isLowerCase(c)) {
hasLower = true;
}
if (Character.isDigit(c)) {
hasDigit = true;
}
if (!Character.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;
}
System.out.println("Length: " + length);
System.out.println("Score: " + score);
The Java details worth a closer look:
if (hasUpper)needs no comparison. Abooleanis already a condition. WritinghasUpper == trueworks but only adds noise.+=.score += 25meansscore = score + 25. Forintit behaves like ordinary addition.- Locals must be initialised.
scoreand every flag start with an explicit value. Java does not give local variables a default, so reading one before assigning it is a compile error. - The same loop as before. The calls
Character.isUpperCase,isLowerCase,isDigitandisLetterOrDigitare static methods taking achar. The!flips the last one so that it selects symbols. - No overflow worry. The score never exceeds 100, far below the largest
int.
Complexity
Time: O(n) for n characters, because the loop is the only part that depends on the password's length; the scoring is five constant-time checks. Space: O(n) for the array made by toCharArray(), plus a few variables.
Edge cases and common mistakes
- Empty password. No flag is set and the length is 0, so the output is
Length: 0andScore: 0. - A space counts as a symbol.
Pass word!2has 11 characters, an uppercase letter, lowercase letters, a digit, and a space and a!as symbols. It scores 100. The symbol points are added once, however many symbols there are. - Points for presence, not for count. A password with ten digits earns the digit points once, because the flag is
trueorfalse. - Using
else iffor the rules. Only one rule would then add points, so a strong password would get at most 25. - Using
=instead of+=.score = 20replaces the points collected so far, so the last rule that fires decides the whole score. - Printing before the score is final. Both output lines come after all the rules have run.
- Wrong weights. The numbers 25, 20, 20, 20 and 15 add up to exactly 100. A typo in one of them shows up as a maximum other than 100.
Testing
Secret!99givesLength: 9andScore: 100.abc123givesLength: 6andScore: 40.Pass word!2givesLength: 11andScore: 100.- The empty string gives
Length: 0andScore: 0.
