Start learning free

LESSON 167 OF 409 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 - Common Password Dictionary in C++

Practice

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

unordered_set<string> common = {"password", "123456", "qwerty", "letmein"};
string password = "qwerty";

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the set common and store the result as a boolean.
  • Print "Common: " followed by the boolean. 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 it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use an unordered_set<string> and call .count(password) (returns 1 if present).

unordered_set<string> common = {"password", "123456", "qwerty", "letmein"};
string password = "qwerty";

cout << "Password: " << password << endl;
bool isCommon = common.count(password) > 0;
cout << "Common: " << (isCommon ? "True" : "False") << endl;
if (isCommon) {
    cout << "Weak: reject" << endl;
} else {
    cout << "Strong: accept" << endl;
}

STEP 1 OF 5
    Ctrl + Enter to check

    Password Strength V5 in C++

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