Problem overview
This version asks four questions about the password: does it contain an uppercase letter, a lowercase letter, a digit and a symbol? A symbol is any character that is neither a letter nor a digit, such as !, # or a space. The program prints the length, then one line per question, then a verdict. The verdict is Strong when the length is at least 8 and all four answers are yes, and Weak otherwise. The version before this one tracked a single flag, for digits.
For Secret!99:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For secret99, which has no capital letter and no symbol:
Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak
Approach: four flags, one pass
Each question gets its own boolean flag: hasUpper, hasLower, hasDigit and hasSymbol. All four start at false. One loop over the characters can update all of them, so the password is read once, not four times.
Inside the loop, each class has its own if, with no else. A single character belongs to at most one of the first three classes, but every test still has to run, because the loop does not know in advance which class a character is in. Using else if would also work for most characters, but separate ifs are simpler to read and cannot skip a test by mistake.
The symbol test is the odd one out: it is defined by exclusion. Character.isLetterOrDigit(c) is true for letters and digits, so !Character.isLetterOrDigit(c) is true for everything else.
Step by step
- Store the length in
lengthand print theLength:line. - Create the first two flags,
hasUpperandhasLower, asbooleanvariables set tofalse. - Create the other two,
hasDigitandhasSymbol, the same way. - Start the loop over
password.toCharArray(), with thecharvariablec. - Uppercase test.
Character.isUpperCase(c)setshasUppertotrue. - Lowercase test.
Character.isLowerCase(c)setshasLower, in its ownifafter the first one. - Digit test.
Character.isDigit(c)setshasDigit. - Symbol test.
!Character.isLetterOrDigit(c)setshasSymbol. - Print the first two answers after the loop, each as
TrueorFalse, chosen with the conditional operator. - Print the last two, keeping the order Upper, Lower, Digit, Symbol.
- Write the verdict. The condition has five parts joined with
&&: the length and the four flags. - Add the
else, which printsWeakfor every password that misses at least one condition.
Reading the example
In Secret!99 the characters are S, e, c, r, e, t, !, 9 and 9, nine in all. The S sets hasUpper, the next five letters set hasLower, the ! sets hasSymbol, and the first 9 sets hasDigit. No flag is ever set back to false, so the order of the characters does not matter: 9!tereS gives the same four answers. Only the final state of the flags is used.
The solution in Java
String password = "Secret!99";
int length = password.length();
System.out.println("Length: " + 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;
}
}
System.out.println("Upper: " + (hasUpper ? "True" : "False"));
System.out.println("Lower: " + (hasLower ? "True" : "False"));
System.out.println("Digit: " + (hasDigit ? "True" : "False"));
System.out.println("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
System.out.println("Strong");
} else {
System.out.println("Weak");
}
The Java details worth a closer look:
- The
Characterhelpers.isUpperCase,isLowerCase,isDigitandisLetterOrDigitare static methods of theCharacterclass. They take acharand return aboolean, so they can be used directly asifconditions. - The
!operator. It flips aboolean. Without it, the lastifwould sethasSymbolfor letters and digits, the opposite of what is wanted. - Unicode. These methods understand letters beyond English.
Character.isLetter('é')istrue, soécounts as a letter here, not a symbol. The underscore is a symbol, becauseCharacter.isLetterOrDigit('_')isfalse. - Printing booleans. A
booleanjoined to a string appears as lowercasetrue, so each line picks its text with? :. The parentheses around the conditional are required. - Long conditions. A chain of
&&istrueonly when every part istrue, and it stops at the firstfalsepart.
Complexity
Time: O(n) for n characters: the loop makes four constant-time checks per character. Space: O(n) for the array from toCharArray(), plus a handful of variables.
Edge cases and common mistakes
- A space is a symbol.
Secret 99has length 9 and all four flags areTrue, so it isStrong. The space is the symbol. - Missing one class.
Secret99has length 8 and uppercase, lowercase and digits, but no symbol. It printsSymbol: FalseandWeak. - Too short, but all four classes. A password such as
Ab1!has every class but only four characters, so it prints fourTruelines afterLength: 4and is stillWeak. - Forgetting the
!on the symbol test. The flag would then say whether the password has a letter or digit, not a symbol. - Treating the rules as one condition. The verdict needs all five conditions, so a password that fails any single one, such as a missing symbol, is
Weakhowever strong it looks otherwise. Testing the flags one at a time in separateifstatements would print several verdicts instead of one. - Printing the flags inside the loop. The lines would repeat once per character. They belong after the closing brace.
Testing
Secret!99gives length 9, fourTruelines andStrong.secret99gives length 8, thenUpper: False,Lower: True,Digit: True,Symbol: FalseandWeak.Secret99gives length 8, thenTrue,True,True,FalseandWeak.Secret 99gives length 9, fourTruelines andStrong.
