Start learning free

LESSON 195 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in C++: Rejecting Common Passwords with a Set

Practice Read the full explanation

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

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in C++, explained

    Problem overview

    Some passwords are weak whatever they contain, because everybody tries them first. The starter code holds a list of well-known passwords in an unordered_set<string> named common, and a password to check. The program prints the password, prints whether it is in the set (True or False), and then prints Weak: reject for a common password or Strong: accept for any other.

    For qwerty, which is in the set, the output is:

    Password: qwerty
    Common: True
    Weak: reject
    

    For Tr0ub4dor, which is not:

    Password: Tr0ub4dor
    Common: False
    Strong: accept
    

    Approach: ask a hash set whether it holds the password

    You could compare the password with each entry of a list, one by one. A set answers the question directly: an unordered_set is a hash table, so checking whether a value is in it takes about the same time whether it holds four entries or four million. That makes it the standard tool for a question of the form "is this value in my collection?".

    The program asks the set once, keeps the answer in a bool, and uses that answer twice: to print the Common line and to choose between the two verdicts.

    Step by step

    1. Print the first line: "Password: ", then password, then endl.
    2. Create bool isCommon from common.count(password) > 0. For a set, count returns 0 or 1, because a set holds each value at most once.
    3. Print the Common line, choosing True or False with isCommon ? "True" : "False".
    4. Write an if (isCommon) whose body prints Weak: reject.
    5. Add the else that prints Strong: accept.

    The solution in C++

    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;
    }
    

    Details worth a closer look:

    • Printing a string. cout prints a string variable just like a literal, so the first line is an ordinary chain of <<.
    • count(...) > 0 turns a number into a bool. The comparison is true when the value is in the set and false otherwise.
    • Why not contains? unordered_set::contains exists only from C++20. With C++17 it does not compile, so the lesson uses count. Another C++17 way is common.find(password) != common.end(), which gives the same answer.
    • Asking does not change the set. count and find only look. The set keeps its four entries after any number of lookups.
    • A bool prints as 1 or 0. That is why the Common line picks its words with the conditional operator, inside parentheses so the << chain stays valid.
    • The if reads the flag directly. if (isCommon) needs no comparison, because the variable is already a bool.

    Complexity

    Time: O(1) on average for the lookup in the hash set, plus time proportional to the password's length to hash and compare it. Space: O(1) beyond the set itself.

    Edge cases and common mistakes

    • Case matters. Qwerty is not the same string as qwerty, so it is not found in the set. The output is Common: False and Strong: accept.
    • An empty password. The empty string is not in the set, so the program prints Password: with nothing after the space, then Common: False and Strong: accept. This version does not check the length.
    • Common is not the same as weak in general. This version only rejects passwords on the list. Anything else is accepted, even a short one.
    • Using count as the whole condition. if (common.count(password)) also works, since 1 is true, but storing a named bool makes the two uses clearer.
    • Printing the flag directly. It would show 1 or 0 instead of True or False.
    • Using []. Sets don't have it. For maps, [] inserts a missing key as a side effect, which is another reason to ask with count or find.

    Testing

    passwordOutput after the first line
    qwertyCommon: True, then Weak: reject
    Tr0ub4dorCommon: False, then Strong: accept
    QwertyCommon: False, then Strong: accept
    (empty string)Common: False, then Strong: accept

    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