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 are told whether the password is easy to guess or crack. The strength should vary from weak, moderate, and strong to different. To check the strength, we must check the following points:
The password length must be at least8characters.
It must contain1lowercase letters.
It must contain1uppercase letters
It must contain a digit
It must contain a special character, such as: !@#$%^&*> <,.+ =-
like there is 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!” which has both uppercase and lowercase letters, numbers, and special characters, and is strong, and its length is greater than8characters, therefore 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 the password as a moderate password. Like the password “w3codebox12like, it will be considered as moderate because it contains lowercase letters, a digit, and its length is greater than8characters.
Input: w3codebox!@12 Output: Password strength:-Strong Explanation: The 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 Step 1 ⇒ In function void printStrength(string& input) Declare and initialize n = input.length() Declare bool hasLower = false, hasUpper = false Declare bool hasDigit = false, specialChar = false Declare string normalChars = "abcdefghijklmnopqrstu" "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"1234567890 " Loop For i = 0 and i < n and i++ If (islower(input[i])) Set hasLower = true If (isupper(input[i])) Set hasUpper = true If (isdigit(input[i])) Set hasDigit = true Set size_t special = input.find_first_not_of(normalChars) If (special != string::npos) Set specialChar = true End Loop Print "Strength of password:"-" If (hasLower && hasUpper && hasDigit && specialChar && (n >= 8)) Print "Strong" else if ((hasLower || hasUpper) && specialChar && (n >= 6)) Print "Moderate" else print "Weak" Step 2 ⇒ In function int main() Declare and initialize input = "w"3codebox!@12" printStrongNess(input) Stop
#include <iostream> using namespace std; void printStrongNess(string& input) { int n = input.length(); //Check the lowercase letters in the 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; } //Password Strength cout << "Strength of password:-"; if (hasLower && hasUpper && hasDigit && specialChar && (n >= 8)) cout << "Strong" << endl; else if ((hasLower || hasUpper) && specialChar && (n >= 6)) cout << "Moderate" << endl; else cout << "Weak" << endl; } int main() { string input = "w3codebox!@12"; printStrongNess(input); return 0; }
Output result
Strength of password:-Moderate