Start learning free

LESSON 191 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in C++: A Length Check

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

string password = "Secret99";

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

password.length() (or password.size()) gives the number of characters.

string password = "Secret99";

int length = password.length();
cout << "Password length: " << length << endl;
if (length >= 8) {
    cout << "OK" << endl;
} else {
    cout << "Too short" << endl;
}

STEP 1 OF 4
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in C++, explained

    Problem overview

    This is the first version of a small password strength checker. It looks at one thing only: how long the password is. The program prints the number of characters on a line, then a verdict on the next: OK when the password has at least 8 characters, Too short otherwise.

    The starter code holds string password = "Secret99";. It has 8 characters, so the output is:

    Password length: 8
    OK
    

    With abc the output is Password length: 3 followed by Too short.

    Approach: measure once, use the number twice

    The length is needed in two places: it is printed, and it is compared with 8. So the program stores it in a variable, length, instead of asking the string twice. Then it prints the label and the number, and finishes with an if/else that chooses between two messages. Exactly one of the two messages is printed for every password.

    Step by step

    1. Create int length from password.length(). A string in C++ is an object, and length() is a method you call with parentheses.
    2. Print the first line. Chain the pieces with <<: the text "Password length: ", then length, then endl. cout prints an integer as its digits, so no conversion is needed.
    3. Write the if with the condition length >= 8 and print OK inside it.
    4. Add the else and print Too short inside it. The closing brace of the if and the word else sit on the same line.

    The solution in C++

    string password = "Secret99";
    
    int length = password.length();
    cout << "Password length: " << length << endl;
    if (length >= 8) {
        cout << "OK" << endl;
    } else {
        cout << "Too short" << endl;
    }
    

    Details worth a closer look:

    • length() and size() are the same. Both return the number of characters stored in the string. The lesson uses length() because it reads naturally for text.
    • The return type is unsigned. The method returns an unsigned integer type, and the code stores it in an int. That conversion is fine for a password, whose length is far below the range of an int. It matters more when you subtract: for an empty string, password.length() - 1 does not give -1, it wraps around to a huge positive number.
    • Chaining with <<. Each << sends one more piece to cout, in order. A string literal, an int and endl can all appear in the same chain.
    • endl ends the line. Without it, OK would be printed right after the digits on the same line.
    • The comparison is >=. At least 8 includes 8, so a password of exactly 8 characters is accepted. With > it would need 9.

    What length() counts

    For a std::string, length() counts bytes, not visible characters. For plain ASCII text such as letters, digits and punctuation, one byte is one character, and spaces count too: pass word has length 9. For a character outside ASCII, the result can be larger than what you see. The word café, written in UTF-8, has length 5, because the é takes two bytes. The lesson's passwords are ASCII, so the count matches what you read.

    Edge cases and common mistakes

    • The boundary. A password of 7 characters prints Too short, and one of 8 prints OK. Check both sides of a boundary whenever you write a comparison.
    • An empty password. "" has length 0, so the output is Password length: 0 and Too short.
    • Forgetting the parentheses. password.length without () names the method instead of calling it, and the code does not compile.
    • Using > instead of >=. It rejects passwords of exactly 8 characters.
    • Printing the verdict on the same line. Leaving out endl after the first message joins the two lines together.

    Testing

    passwordOutput
    Secret99Password length: 8, then OK
    abcPassword length: 3, then Too short
    abcdefghPassword length: 8, then OK
    abcdefgPassword length: 7, then Too short
    (empty string)Password length: 0, then Too short

    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