Start learning free

LESSON 196 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in C#: Four Character-Class Flags

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

string password = "Secret!99";

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Start four flags (hasUpper, hasLower, hasDigit, hasSymbol) at false. Loop over every character and set the matching flag to true.
  • A symbol is any character that is not a letter and not a digit.
  • Print one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and all four flags are true (stricter than the last lesson), print Strong; otherwise print Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

Use char.IsUpper(c), char.IsLower(c), char.IsDigit(c) and char.IsLetterOrDigit(c) inside foreach (char c in password).

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");
}

STEP 1 OF 13
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in C#, explained

    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

    1. Store and print the length.
    2. Create the four flags, all bool and all starting at false: "not seen yet".
    3. Loop over the characters with foreach (char c in password).
    4. Uppercase. char.IsUpper(c) sets hasUpper.
    5. Lowercase. char.IsLower(c) sets hasLower.
    6. Digit. char.IsDigit(c) sets hasDigit.
    7. Symbol. char.IsLetterOrDigit(c) is true for letters and digits, so !char.IsLetterOrDigit(c) is true for everything else. That sets hasSymbol.
    8. Print the four flag lines after the loop, using the ? : operator to write True or False.
    9. Write the verdict. Five conditions joined with &&: the length and one per flag. The else prints Weak for 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 char helpers are static methods. They are called on the type, as in char.IsUpper(c), and each takes a char and returns a bool. char is the C# keyword for the System.Char type.
    • ! flips a bool. 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 convert string to bool. A bool joined to a string prints as True or False in C# anyway, so the ternary here makes the wording explicit rather than fixing a formatting problem.
    • The flags are bool, not int. C# has no truthy numbers, so hasUpper = 1 doesn'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 still Weak, 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 is Strong.
    • Underscore is a symbol. char.IsLetterOrDigit('_') is false, so _ sets hasSymbol.
    • 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 is Weak: 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 prints Length: 0, four False lines and Weak.

    Testing

    • "Secret!99" gives all four True lines and Strong.
    • "secret99" gives Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • "Secret 99" gives all four True lines and Strong.
    • "Aa1!" gives four True lines and Weak.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free