Start learning free

LESSON 199 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in C#: A 0 to 100 Score

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

string password = "Secret!99";

Build:

Length: 9
Score: 100

Rules:

  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use the same char.IsUpper, char.IsLower, char.IsDigit, char.IsLetterOrDigit checks, then accumulate into int score.

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

STEP 1 OF 18
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in C#, explained

    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

    1. Store the length in length. It is used for the length rule and for the first output line.
    2. Create the four flags, hasUpper, hasLower, hasDigit and hasSymbol, each a bool that starts at false.
    3. Loop over the characters. A foreach (char c in password) runs the body once per character, 9 times for this password.
    4. Set the flags. Four if statements use char.IsUpper, char.IsLower, char.IsDigit and !char.IsLetterOrDigit. A flag only ever changes to true.
    5. Create the score. int score = 0; goes after the loop. The points are added to it next.
    6. Award the points. length >= 8 adds 25, then hasUpper, hasLower and hasDigit add 20 each, and hasSymbol adds 15.
    7. Print the two lines once the score is final: Length: with the length, then Score: 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 += 25 means score = score + 25. It changes the variable in place.
    • A bool works as a condition on its own. if (hasUpper) needs no comparison, and writing if (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 score appears after the flags it depends on. Declaring it a few lines earlier would work equally well, as long as it starts at 0.
    • The int never overflows here. The score is at most 100, and an int holds 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 is Length: 0 and Score: 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 if for 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" gives Length: 9 and Score: 100.
    • "abc123" gives Length: 6 and Score: 40.
    • "ABCDEFGH" gives Length: 8 and Score: 45.
    • "Aa1!" gives Length: 4 and Score: 75.
    • "" gives Length: 0 and Score: 0.
    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