You work at a growing e-commerce website, which has also become a target for thieves. Thieves gain access to customers' accounts by guessing passwords, which is often easy (like "secret," "password," and "1234"). If your customers use stronger passwords, your company can save some effort in dealing with theft problems.
Your task is to design a password analyzer to inform customers about the strength of their chosen passwords. A "strong" password increases the difficulty of guessing by increasing length or mixing letters, numbers, and symbols. In this task, a "strong" password possesses all of the following characteristics:
- At least 8 characters in length (e.g., "spookyfish")
- Contains both uppercase and lowercase letters (e.g., "sPookyFISH")
- Contains both letters and at least one number or symbol (e.g., "sPookyFiSH3" or "$PookyFI3H")
A "good" password has two of the above characteristics, an "acceptable" password has only one characteristic. A password that does not meet any of the above characteristics is considered "weak". Please write a program to analyze the strength of the given password.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF inputs: each test case consists of a single line containing a password, with a maximum length of 30 characters.
There are no spaces in the password. | Based on the criteria mentioned above, output the strength of the input password. |
lizard aardvark Aardvark Aardvark77 | This password is WEAK This password is ACCEPTABLE This password is GOOD This password is STRONG |
Thought Process
Declare an ans variable to calculate how many requirements the password meets.
If the length of the string is greater than or equal to 8, then increment ans by one.
Declare four boolean variables: up, down, alpha, and other, all initially set to false. They represent whether there are uppercase letters, lowercase letters, alphabetic characters, and other characters, respectively. For each character in the password, use isalpha, isupper, and islower to determine whether it is an alphabetic character and whether it is uppercase or lowercase. If it is not an alphabetic character, set other to true.
If both up and down are true, then increment ans.
If both alpha and other are true, then increment ans.
Finally, output the specified answer based on how many requirements are met.
Sample Code-ZeroJudge A624: Password Analyzer
#include <iostream>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
map<int, string>MAP;
MAP[0] = "WEAK";
MAP[1] = "ACCEPTABLE";
MAP[2] = "GOOD";
MAP[3] = "STRONG";
string str;
while (cin >> str)
{
int ans = 0, len = int(str.length());
if (len >= 8) ans++;
bool up = false, down = false, alpha = false, other = false;
for (int i = 0; i<len; i++)
{
if (isalpha(str[i]))
{
alpha = true;
if (isupper(str[i])) up = true;
if (islower(str[i])) down = true;
}
else other = true;
}
if (up && down) ans++;
if (alpha && other) ans++;
cout << "This password is " << MAP[ans] << "\n";
}
}
//ZeroJudge A624
//Dr. SeanXD