Start learning free

LESSON 196 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in C++: A 0 to 100 Score from Flags

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 <cctype> helpers, 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;
for (char c : password) {
    if (isupper(c)) {
        hasUpper = true;
    }
    if (islower(c)) {
        hasLower = true;
    }
    if (isdigit(c)) {
        hasDigit = true;
    }
    if (!isalnum(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;
}
cout << "Length: " << length << endl;
cout << "Score: " << score << endl;

STEP 1 OF 18
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in C++, explained

    Problem overview

    The last version of the project combines the earlier checks into one number. The program starts a score at 0 and adds points for each rule the password meets:

    • 25 points for a length of 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.

    It then prints the length and the score. For Secret!99 every rule passes, and 25 + 20 + 20 + 20 + 15 is 100:

    Length: 9
    Score: 100
    

    For abc123 only the lowercase letters and the digits count, so the output is Length: 6 and Score: 40.

    Approach: collect the facts, then score them

    The program has two phases. First it gathers facts about the password, with the same technique as the earlier versions: a loop over the characters that flips four bool flags. Then it turns the facts into points, with one if per rule that adds to score.

    Keeping the phases apart makes each one easy to read. The loop answers "what is in the password?" and the scoring answers "what is that worth?". Changing a rule's weight touches one line in the second phase and nothing in the first.

    Step by step

    1. Create length from password.length(). It is used for the length rule and for the first output line.
    2. Create hasUpper, hasLower, hasDigit and hasSymbol as bool flags, each false: "not seen yet".
    3. Open a range-based for with char c over password.
    4. Inside it, test isupper(c), islower(c), isdigit(c) and !isalnum(c), each in its own if, and set the matching flag.
    5. Create int score = 0; after the loop.
    6. Add the length rule: if (length >= 8) then score += 25.
    7. Add the three 20-point rules for hasUpper, hasLower and hasDigit.
    8. Add the 15-point rule for hasSymbol.
    9. Print Length: and the length, then print Score: and the score, each with endl.

    The solution in C++

    string password = "Secret!99";
    
    int length = password.length();
    bool hasUpper = false;
    bool hasLower = false;
    bool hasDigit = false;
    bool hasSymbol = false;
    for (char c : password) {
        if (isupper(c)) {
            hasUpper = true;
        }
        if (islower(c)) {
            hasLower = true;
        }
        if (isdigit(c)) {
            hasDigit = true;
        }
        if (!isalnum(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;
    }
    cout << "Length: " << length << endl;
    cout << "Score: " << score << endl;
    

    Details worth a closer look:

    • Every rule is its own if. An else if chain would stop after the first rule that matched, so a password could never earn points from more than one rule. Independent if statements let all five rules contribute.
    • score += 25 is shorthand for score = score + 25.
    • The weights add up to 100. 25 + 20 + 20 + 20 + 15 is 100, which is why the score is always between 0 and 100 and no clamping is needed.
    • Flags are read like conditions. if (hasUpper) needs no == true: the variable is already a bool.
    • Symbols by elimination. !isalnum(c) means "not a letter and not a digit", so a space counts as a symbol.
    • Non-ASCII input. The character functions expect a value that fits in an unsigned char. A plain char holding a byte above 127 is usually negative, which makes the call undefined behaviour. Passing (unsigned char)c is the safe form. The lesson's passwords are ASCII.

    Adding up the points

    The score is a sum of independent contributions, so it helps to see which rule paid what. For Secret!99 (9 characters), the length rule gives 25, the capital S gives 20, the lowercase letters give 20, the 9s give 20 and the ! gives 15, for a total of 100. For abc123 (6 characters) the length rule fails, there is no capital letter and no symbol, and the lowercase letters and digits give 20 + 20 = 40.

    Why the score is not computed inside the loop

    It is tempting to add points as soon as a character matches, but that would pay the same rule once per matching character: a password with five lowercase letters would earn 100 points for them. The flags prevent this. A flag is either set or not, however many characters set it, so each rule is paid at most once. That is also why the scoring comes after the loop: the points are decided only when the whole password has been seen, and the loop's only job is to record which classes appeared.

    Complexity

    Time: O(n) for a password of n characters: one pass with four tests per character, and five comparisons afterwards. Space: O(1): a length, four flags and a score.

    Edge cases and common mistakes

    • An empty password. No flag is set and the length rule fails, so the output is Length: 0 and Score: 0.
    • Long but one-dimensional. ABCDEFGH and abcdefgh both have 8 characters and one letter class, so each scores 25 + 20 = 45.
    • Short but varied. A password can earn points from the classes while failing the length rule, as abc123 does.
    • Using else if between the scoring rules. It would add at most one amount to the score.
    • Counting characters instead of classes. The score rewards having a class, not having many characters of it, so a flag is the right tool and a counter is not needed.
    • Printing the score before the length. The output lists the length line first.

    Testing

    passwordOutput
    Secret!99Length: 9, Score: 100
    abc123Length: 6, Score: 40
    (empty string)Length: 0, Score: 0
    ABCDEFGHLength: 8, Score: 45
    abcdefghLength: 8, Score: 45
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free