Start learning free

LESSON 192 OF 281 · 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 isupper, islower, isdigit, isalnum from <cctype>.

string password = "Secret!99";

int length = password.length();
cout << "Length: " << length << endl;
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;
    }
}
cout << "Upper: " << (hasUpper ? "True" : "False") << endl;
cout << "Lower: " << (hasLower ? "True" : "False") << endl;
cout << "Digit: " << (hasDigit ? "True" : "False") << endl;
cout << "Symbol: " << (hasSymbol ? "True" : "False") << endl;
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
    cout << "Strong" << endl;
} else {
    cout << "Weak" << endl;
}

STEP 1 OF 13
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in C++, explained

    Problem overview

    This version asks what kinds of characters the password contains: an uppercase letter, a lowercase letter, a digit, a symbol. The program prints the length, one line per kind (Upper, Lower, Digit, Symbol, each True or False), and a verdict. A symbol is any character that is neither a letter nor a digit. The verdict is Strong only when the length is at least 8 and all four kinds are present; otherwise it is Weak.

    For Secret!99 every kind is present:

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

    The previous version tracked a single flag for digits. This one repeats the idea four times in the same loop.

    Approach: four flags, one pass

    Each kind of character gets its own bool flag, hasUpper, hasLower, hasDigit and hasSymbol, all starting at false. A single loop over the characters tests each character against all four kinds and flips the matching flag to true. After the loop, the flags are printed and the verdict is computed from them.

    The four tests are separate if statements, not an else if chain. A character is only one kind, so at most one test will succeed for it, but that does not matter: every test is asked of every character, and the code stays uniform and easy to extend.

    Step by step

    1. Create length from password.length().
    2. Print Length: and the number.
    3. Create hasUpper and hasLower as bool, both false.
    4. Create hasDigit and hasSymbol the same way.
    5. Open a range-based for with char c over password.
    6. Test isupper(c) and set hasUpper.
    7. Test islower(c) and set hasLower.
    8. Test isdigit(c) and set hasDigit.
    9. Test !isalnum(c) and set hasSymbol. isalnum is true for letters and digits, so the ! turns it into "neither".
    10. After the loop, print the Upper and Lower lines, choosing True or False with ? :.
    11. Print the Digit and Symbol lines in the same way, keeping the order Upper, Lower, Digit, Symbol.
    12. Write the verdict's if: the length test and the four flags joined with &&, printing Strong.
    13. Add the else and print Weak.

    The solution in C++

    string password = "Secret!99";
    
    int length = password.length();
    cout << "Length: " << length << endl;
    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;
        }
    }
    cout << "Upper: " << (hasUpper ? "True" : "False") << endl;
    cout << "Lower: " << (hasLower ? "True" : "False") << endl;
    cout << "Digit: " << (hasDigit ? "True" : "False") << endl;
    cout << "Symbol: " << (hasSymbol ? "True" : "False") << endl;
    if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
        cout << "Strong" << endl;
    } else {
        cout << "Weak" << endl;
    }
    

    Details worth a closer look:

    • The classification functions. isupper, islower, isdigit and isalnum each take one character and answer a yes-or-no question about it. With ASCII text they behave as their names suggest.
    • Symbols by elimination. There is no function that asks "is this a symbol" in the lesson's sense, so the code defines it as "not a letter and not a digit". That includes spaces: !isalnum(' ') is true, so a space counts as a symbol.
    • The conditional operator and parentheses. Each (hasUpper ? "True" : "False") is wrapped in parentheses so that the << chain around it still works. cout would print the bare flag as 1 or 0.
    • Five conditions, one && chain. length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol is true only when each part is true. One missing kind, or a short password, gives Weak.
    • Non-ASCII input. The classification 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.

    Watching the flags flip

    Printing the four flags after each character of Secret!99 (as 1 for true and 0 for false, in the order upper, lower, digit, symbol) gives:

    CharacterFlags after it
    S1000
    e1100
    c, r, e, t1100 (no change)
    !1101
    91111
    91111 (no change)

    Flags only ever go from 0 to 1. The capital S sets the first, the first lowercase letter sets the second, the ! sets the symbol flag, and the first 9 completes the set. Characters that repeat a kind change nothing. At the end of the loop the program knows all four facts without having stored the characters themselves.

    Complexity

    Time: O(n) for n characters: each one gets four constant-time tests. Space: O(1): four flags and a length.

    Edge cases and common mistakes

    • Length is not enough. Ab1! contains all four kinds, but it has 4 characters, so the verdict is Weak.
    • One kind missing. Secret99 has 8 characters with upper, lower and digit, but no symbol, so it is Weak.
    • Using else if between the four tests. It happens to give the same flags here, because a character is only one kind. It also stops the code from working the day a character should count for two things, which is why the lesson uses separate if statements.
    • Forgetting the ! in the symbol test. isalnum(c) alone would flag letters and digits as symbols, and every normal password would show Symbol: True.
    • Printing flags in a different order. The output must list Upper, Lower, Digit, Symbol.
    • Treating a space as a letter. It is neither a letter nor a digit, so it counts as a symbol.

    Testing

    passwordUpperLowerDigitSymbolVerdict
    Secret!99TrueTrueTrueTrueStrong (Length: 9)
    secret99FalseTrueTrueFalseWeak (Length: 8)
    Secret99TrueTrueTrueFalseWeak (Length: 8)
    Ab1!TrueTrueTrueTrueWeak (Length: 4)

    The next version builds on this program.

    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