C++ Code Example: check password strength

In the following example, a password is checked for its strength. If the password contains less than 6 characters, it is invalid. If the password contains less than 10 characters it gets the password strength medium, if it contains more than 10 characters it is strong.

Furthermore, the password must contain at least one lowercase letter, one uppercase letter, one number and one special character. The output of this program is the strength of the password, or an indication of a missing character.

Code Explanation

This code is a program to check the strength of a password entered by the user. The program first takes in a string as the password, and then passes it to the function checkPassword for checking the strength of the password.

The function checkPassword checks the password by initializing five variables, upperChars, lowerChars, specialChars, digits, and length to keep track of the number of upper-case characters, lower-case characters, special characters, digits, and the length of the password, respectively.

The program first checks if the length of the password is less than 6 characters. If the length is less than 6, it returns a message that the password must be at least 6 characters long. If the length of the password is equal to or greater than 6 characters, the program enters a for loop to iterate through each character of the password. For each character, the program checks if it is an upper-case letter, a lower-case letter, a digit or a special character, and increments the corresponding variable.

After the for loop, the program checks if the password contains at least one upper-case letter, one lower-case letter, one special character, and one digit. If the password contains all of these, then it checks if the length of the password is greater than or equal to 10 characters. If it is, then the program prints that the password strength is strong. If the length of the password is less than 10 characters, the program prints that the password strength is medium.

If the password does not contain at least one upper-case letter, one lower-case letter, one special character, and one digit, the program prints a message indicating which type of character is missing in the password.

Finally, the main function calls the checkPassword function and passes the password entered by the user as an argument.

#include <iostream>
#include <cstring>
using namespace std;

void checkPassword(string password) {
    int upperChars = 0, lowerChars = 0, specialChars = 0, digits = 0, length;
    length = password.length();

    if (length < 6) {
        cout << "Password must be at least 6 characters long!" << endl;
        return;
    } else {
        for (int i = 0; i < length; i++) {
            if (isupper(i)) {
                upperChars++;
            } else if (islower(i)) {
                lowerChars++;
            } else if (isdigit(i)) {
                digits++;
            } else {
                specialChars++;
            }
        }
    }

    if (upperChars != 0 && lowerChars != 0 && digits != 0 && specialChars != 0) {
        if (length >= 10) {
            cout << "The strength of password is strong." << endl;
        } else {
            cout << "The strength of password is medium." << endl;
        }
    } else {
        if (upperChars == 0)
            cout << "Password must contain at least one uppercase character!" << endl;
        if (lowerChars == 0)
            cout << "Password must contain at least one lowercase character!" << endl;
        if (specialChars == 0)
            cout << "Password must contain at least one special character!" << endl;
        if (digits == 0)
            cout << "Password must contain at least one digit!" << endl;
    }
}


int main() {
    string password;
    cout << "Please enter a password: " << endl;
    cin >> password;

    checkPassword(password);

    return 0;
}
Output
[first_run]
Please enter password: 34rT%
Password must be at least 6 characters long!

[second_run]
Please enter password: 23456weretsDFDS
Password must contain at least one special character!

[third_run]
Please enter password: 456ztr7TZ$$
The strength of password is strong.