# Program to check Strength of Password in C++

C++Server Side ProgrammingProgramming

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 that the password is whether easily guessed or cracked. The strength should vary from weak, average and strong. To check the strength we have to check the following points −

• Password must be at least 8 characters long.
• It must contain 1 lower case alphabet.
• It must contain 1 uppercase alphabet
• It must contain a digit
• It must contain a special character like : !@#\$%^&*()><,.+=-

Like there is a password “tutorialspoint” which is easily guessed so we can cay that he given password is “weak” as it contains only lowercase characters, whereas password “Tutorialspoint@863!” is strong as have both upper and lowercase, a digit and a special character and is longer than 8 characters, hence meeting all the conditions to make a password stronger.

If there is some password meeting the more than half of the characteristics of the strong password, then we will consider the password as moderate. Like the password “tutorialspoint12” it will be considered as moderate as contains lowercase, a digit and its length is greater than 8 chacters.

## Example

Input: tutoriAlspOint!@12
Explanation: Password has 1 lowercase, 1 uppercase, 1 special character, more than 8 characters long and a digit, hence the password is strong.
Input: tutorialspoint
Output: Strength of password:-Weak

Approach we will be using to solve the given problem

• Take a string output for password.
• Check the password for all the factors which are responsible for judging the password strength.
• According to the factors print the password’s strength.

## Algorithm

Start
Step 1 ⇒ In function void printStrongNess(string& input)
Declare and initialize n = input.length()
Declare bool hasLower = false, hasUpper = false
Declare bool hasDigit = false, specialChar = false
Declare string normalChars = "abcdefghijklmnopqrstu"
"vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
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
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 = "tutorialspoint!@12"
printStrongNess(input)
Stop

## Example

#include <iostream>
using namespace std;
void printStrongNess(string& input) {
int n = input.length();
// Checking lower alphabet in string
bool hasLower = false, hasUpper = false;
bool hasDigit = false, specialChar = false;
string normalChars = "abcdefghijklmnopqrstu" "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
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;
}
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 = "tutorialspoint!@12";
printStrongNess(input);
return 0;
}

## Output

Strength of password:-Moderate