English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Given a string input containing password characters, the task is to check the strength of the password.
The strength of the password is when you tell you whether the password is easy to guess or crack. The strength should vary from weak, moderate, and strong.
The password length must be at least8characters.
It must contain1lowercase.
It must contain1uppercase
It must contain a digit
It must contain a special character, such as: !@#$%^&*> <,.+ =-
like a password “w3codebox” is easy to guess, so we can conclude that the password “weak” he provided is because it only contains lowercase letters, while the password “w3codebox @ 863!” It has both uppercase and lowercase letters, numbers, special characters, and strength, and its length is greater than8characters, thus satisfying all conditions that make the password stronger.
If some passwords have more than half of the features of a strong password, then we will consider it a moderate password. Like the password “w3codebox12Like a password with more than 8 characters, it will be considered moderate because it contains lowercase letters, a digit, and its length is greater than8characters.
Input: w3codebox!@12 Output: Password strength:-Strong Explanation: Password has 1 lowercase, 1 uppercase, 1 special character, more than 8 characters long and a digit, hence the password is strong. Input: w3codebox Output: Password strength:-Weak
The method we will use to solve the given problem-
Output the string as the password.
Check if all factors can be used to judge the strength of the password.
Print the strength of the password based on factors.
Start Schritt 1 ⇒ In function void printStrength(string& input) Declare and initialize n = input.length() Erkläre bool hasLower = false, hasUpper = false Erkläre bool hasDigit = false, specialChar = false Erkläre string normalChars = "abcdefghijklmnopqrstu" "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"1234567890 " Schleife Für i = 0 und i < n und i++ Wenn (islower(input[i])) Setze hasLower = true Wenn (isupper(input[i])) Setze hasUpper = true Wenn (isdigit(input[i])) Setze hasDigit = true Setze size_t special = input.find_first_not_of(normalChars) Wenn (special != string::npos) Setze specialChar = true Ende des Loops Drucke "Stärke des Passworts:"-" Wenn (hasLower && hasUpper && hasDigit && specialChar && (n >= 8)) Drucke "Stark" else if ((hasLower || hasUpper) && specialChar && (n >= 6)) Drucke "Mittel" else print "Weak" Schritt 2 ⇒ In der Funktion int main() Declare und initialisiere input = "w"3codebox!@12" printStrength(input) Stop
#include <iostream> using namespace std; void printStrength(string& input) { int n = input.length(); //Überprüfen Sie die niedrigen Buchstaben im String bool hasLower = false, hasUpper = false; bool hasDigit = false, specialChar = false; string normalChars = "abcdefghijklmnopqrstu" "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"1234567890 "; for (int i = 0; i < n; i++) { if (islower(input[i])) hasLower = true; if (isupper(input[i])) hasUpper = true; if (isdigit(input[i])) hasDigit = true; size_t special = input.find_first_not_of(normalChars); if (special != string::npos) specialChar = true; } //Passwortstärke cout << "Stärke des Passworts:-"; if (hasLower && hasUpper && hasDigit && specialChar && (n >= 8)) cout << "Stark" << endl; else if ((hasLower || hasUpper) && specialChar && (n >= 6)) cout << "Mäßig" << endl; else cout << "Weak" << endl; } int main() { string input = "w3codebox!@12"; printStrongNess(input); return 0; }
Ausgaberesultat
Stärke des Passworts:-Mäßig