Start learning free

LESSON 192 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in C++: Has Digit with a bool Flag

Practice Read the full explanation

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

string password = "Secret99";

Build:

Length: 8
Has digit: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag hasDigit, which starts as false and becomes true when a digit is seen.
  • Print "Has digit: " 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 hasDigit is true, print Strong; otherwise print Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

isdigit(c) from <cctype> (already included) works on a char.

string password = "Secret99";

int length = password.length();
cout << "Length: " << length << endl;
bool hasDigit = false;
for (char c : password) {
    if (isdigit(c)) {
        hasDigit = true;
    }
}
cout << "Has digit: " << (hasDigit ? "True" : "False") << endl;
if (length >= 8 && hasDigit) {
    cout << "Strong" << endl;
} else {
    cout << "Weak" << endl;
}

STEP 1 OF 8
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in C++, explained

    Problem overview

    The checker gets a second rule. A password is now Strong only if it has at least 8 characters and contains at least one digit; otherwise it is Weak. The program prints three lines: the length, whether a digit was found (written as True or False), and the verdict.

    For Secret99 the output is:

    Length: 8
    Has digit: True
    Strong
    

    For abcdefgh the length passes but there is no digit:

    Length: 8
    Has digit: False
    Weak
    

    The previous version only compared the length. This one has to look inside the string.

    Approach: a flag that flips once

    To find out whether any character is a digit, the program makes a bool flag, hasDigit, that starts as false: no digit seen yet. It then goes through the password one character at a time, and the first time a character is a digit it sets the flag to true. Nothing ever sets it back to false, so once a digit has been seen the flag stays true, no matter what characters follow.

    When the loop ends, the flag answers the question for the whole password, and the verdict combines it with the length test using &&.

    Step by step

    1. Create length from password.length(). It is used in the first output line and in the verdict.
    2. Print Length: followed by the number.
    3. Create bool hasDigit = false;, the "not seen yet" state.
    4. Write a range-based for where the char c goes over password. It hands you one character per pass.
    5. Inside the loop, test isdigit(c) and set hasDigit to true when it holds.
    6. After the loop, print Has digit: and the flag, as the text True or False.
    7. Write the verdict's if with length >= 8 && hasDigit and print Strong.
    8. Add the else and print Weak.

    The solution in C++

    string password = "Secret99";
    
    int length = password.length();
    cout << "Length: " << length << endl;
    bool hasDigit = false;
    for (char c : password) {
        if (isdigit(c)) {
            hasDigit = true;
        }
    }
    cout << "Has digit: " << (hasDigit ? "True" : "False") << endl;
    if (length >= 8 && hasDigit) {
        cout << "Strong" << endl;
    } else {
        cout << "Weak" << endl;
    }
    

    Details worth a closer look:

    • Range-based for. for (char c : password) means "for each character c in password". It gives you a copy of each character, in order, with no index to manage.
    • isdigit. It is true for the characters '0' to '9' and false for everything else, including letters, spaces and symbols. It comes from the C character functions and takes one character.
    • A bool prints as a number. cout << true prints 1 and cout << false prints 0. The lesson needs the words True and False, so it picks the text itself with the conditional operator: hasDigit ? "True" : "False" is the first text when the flag is true and the second when it is false.
    • Parentheses around the conditional. The expression sits inside ( ... ) in the cout chain because << binds more tightly than ? :. Without them the compiler groups cout << "Has digit: " << hasDigit as the condition and tries to apply << to "False" and endl, which fails with an error about invalid operands to operator<<.
    • && needs both sides. The verdict is Strong only when the length test and the flag are both true. Either one failing, or both, gives Weak.
    • The loop doesn't stop early. It checks every character even after a digit was found. For a password this costs next to nothing, and it keeps the code simple: there is no break to reason about.

    Why one flag is enough

    The flag stores a single fact about everything seen so far: has a digit appeared yet? That is all the verdict needs. Which character was the digit, or how many digits there were, does not change the answer, so the program doesn't have to remember which character was the digit or where it was. Following Secret99, the flag stays false for the letters S, e, c, r, e, t, becomes true at the first 9 and remains true at the second. For abcdefgh it never changes.

    Complexity

    Time: O(n) for a password of n characters, since each character is examined once. Space: O(1): the program stores a length and one flag, whatever the password size.

    Edge cases and common mistakes

    • Digits only. 12345678 has 8 characters and a digit, so it is Strong under this version's two rules. Letters and symbols are not checked yet.
    • Too short but with a digit. a1 prints Length: 2, Has digit: True and Weak: the digit alone is not enough.
    • Setting the flag back to false. An else branch that resets hasDigit on every non-digit would make the flag reflect only the last character.
    • Printing the flag directly. cout << hasDigit prints 1 or 0, not True or False.
    • Using || in the verdict. It would accept a short password that has a digit, or a long one without any.
    • Non-ASCII input. isdigit expects a value that fits in an unsigned char. A plain char holding a byte above 127 is usually negative, and passing it is undefined behaviour. Casting with (unsigned char)c avoids that; the lesson's passwords are ASCII.

    Testing

    passwordOutput
    Secret99Length: 8, Has digit: True, Strong
    abcdefghLength: 8, Has digit: False, Weak
    12345678Length: 8, Has digit: True, Strong
    a1Length: 2, Has digit: True, Weak

    The next version builds on this program.

    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free