Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?
You start with:
string password = "Secret!99";
Build:
Length: 9 Upper: True Lower: True Digit: True Symbol: True Strong
Rules:
- Print
"Length: "followed by the number of characters. - Start four flags (
hasUpper,hasLower,hasDigit,hasSymbol) atfalse. Loop over every character and set the matching flag totrue. - A symbol is any character that is not a letter and not a digit.
- Print one line per flag, in this order:
"Upper: ","Lower: ","Digit: ","Symbol: ", each followed by the flag. Write it asTrueorFalsewith a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself). - If the length is at least 8 and all four flags are
true(stricter than the last lesson), printStrong; otherwise printWeak.
Try other passwords: with secret99 the output is
Length: 8 Upper: False Lower: True Digit: True Symbol: False Weak
Use
isupper, islower, isdigit, isalnum from <cctype>.
string password = "Secret!99";
int length = password.length();
cout << "Length: " << length << endl;
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSymbol = false;
for (char c : password) {
if (isupper(c)) {
hasUpper = true;
}
if (islower(c)) {
hasLower = true;
}
if (isdigit(c)) {
hasDigit = true;
}
if (!isalnum(c)) {
hasSymbol = true;
}
}
cout << "Upper: " << (hasUpper ? "True" : "False") << endl;
cout << "Lower: " << (hasLower ? "True" : "False") << endl;
cout << "Digit: " << (hasDigit ? "True" : "False") << endl;
cout << "Symbol: " << (hasSymbol ? "True" : "False") << endl;
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
cout << "Strong" << endl;
} else {
cout << "Weak" << endl;
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
