Problem overview
This version looks at what kinds of characters the password contains. It tracks four bool flags: hasUpper for an uppercase letter, hasLower for a lowercase letter, hasDigit for a digit and hasSymbol for a symbol. A symbol is any character that is neither a letter nor a digit.
The program prints the length, one line per flag in the order Upper, Lower, Digit, Symbol, and a verdict. The password is Strong only when its length is at least 8 and all four flags are true. For "Secret!99":
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For "secret99" the length is 8, but there is no uppercase letter and no symbol, so the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
Approach: one pass, four independent tests
The previous lesson used one flag and one test per character. The same idea scales to four flags in a single loop. For each character, the loop runs four separate if statements, and each one may set its own flag.
They are separate if statements, not an if/else if chain. The four tests ask four independent questions, and writing each as its own if says exactly that: every test runs for every character. Each of the four classes is exclusive of the others, so a chain would give the same result here, but it would also tie the tests together, and adding a fifth class later could silently change when the others run.
After the loop, the flags are printed and combined with the length in one long condition.
Step by step
- Store and print the length.
- Create the four flags, all
booland all starting atfalse: "not seen yet". - Loop over the characters with
foreach (char c in password). - Uppercase.
char.IsUpper(c)setshasUpper. - Lowercase.
char.IsLower(c)setshasLower. - Digit.
char.IsDigit(c)setshasDigit. - Symbol.
char.IsLetterOrDigit(c)is true for letters and digits, so!char.IsLetterOrDigit(c)is true for everything else. That setshasSymbol. - Print the four flag lines after the loop, using the
? :operator to writeTrueorFalse. - Write the verdict. Five conditions joined with
&&: the length and one per flag. TheelseprintsWeakfor a password that misses any of them.
The solution in C#
string password = "Secret!99";
int length = password.Length;
Console.WriteLine("Length: " + 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;
}
}
Console.WriteLine("Upper: " + (hasUpper ? "True" : "False"));
Console.WriteLine("Lower: " + (hasLower ? "True" : "False"));
Console.WriteLine("Digit: " + (hasDigit ? "True" : "False"));
Console.WriteLine("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
Console.WriteLine("Strong");
} else {
Console.WriteLine("Weak");
}
The C# details worth a closer look:
- The
charhelpers are static methods. They are called on the type, as inchar.IsUpper(c), and each takes acharand returns abool.charis the C# keyword for theSystem.Chartype. !flips abool. In!char.IsLetterOrDigit(c)the!applies to the result of the call, so the condition is true for characters such as!,#,_and the space.- The ternary needs its parentheses.
"Upper: " + (hasUpper ? "True" : "False")evaluates the ternary first. Without the parentheses,+binds tighter and the compiler reports that it cannot convertstringtobool. Abooljoined to a string prints asTrueorFalsein C# anyway, so the ternary here makes the wording explicit rather than fixing a formatting problem. - The flags are
bool, notint. C# has no truthy numbers, sohasUpper = 1doesn't compile.
Letters beyond ASCII
These methods understand Unicode. char.IsUpper('É') is true, so accented capitals count as uppercase. A letter from a script without upper and lower case, such as '中', is a letter (IsLetterOrDigit is true) but is neither upper nor lower, so it sets none of the first three flags. It is not a symbol either.
Emoji and other characters outside the basic range are stored as two char values, a surrogate pair. Neither half is a letter or digit, so the loop sees two symbols. For a yes or no answer to "is there a symbol", that makes no difference.
Edge cases and common mistakes
- Long, but missing a class.
"secret99"has all the length it needs and is stillWeak, because two flags are false. - The space is a symbol.
"Secret 99"has a length of 9 and a space, which counts as a symbol, so it isStrong. - Underscore is a symbol.
char.IsLetterOrDigit('_')isfalse, so_setshasSymbol. - A wrong symbol test. Defining a symbol as "not upper and not lower and not digit" would wrongly count letters such as
'中'. Testing "not a letter and not a digit" is the right form. - Short but complete.
"Aa1!"has all four classes and a length of 4, so it isWeak: the length rule is part of the verdict. - Printing in the wrong order. The lines must be Upper, Lower, Digit, Symbol, whatever order the loop tests them in.
- Empty password.
""leaves all four flags false and printsLength: 0, fourFalselines andWeak.
Testing
"Secret!99"gives all fourTruelines andStrong."secret99"givesUpper: False,Lower: True,Digit: True,Symbol: FalseandWeak."Secret 99"gives all fourTruelines andStrong."Aa1!"gives fourTruelines andWeak.
