Password strength checker in C++
In this tutorial, we will learn how to create a password strength checker in C++. We need our users to create a strong password so that their data is more secure. A strong password is supposed to contain uppercase English letters, lowercase English letters, special characters, digits, etc. Let’s gather more knowledge about a strong password and see how we can create a program to check if it is so.
Password strength checker in C++
Let’s define a strong password for our users. A password is strong if:
- It contains at least one uppercase letter.
- It contains at least one lowercase letter.
- It contains at least one digit.
- It has at least one character which is neither a letter nor a number.
- Its length must be more than 8 characters.
Let’s say if a password satisfies all the above conditions, it is a strong password. If a password is not strong and satisfies at least three of the first four conditions and its length is greater than or equal to 6 characters, then it is a moderate password. In all other cases, the password is weak.
Let’s understand this with some examples.
Example 1
Password: CodeSpeedy@1213
Since the above password satisfies all the specified conditions, it is a strong password.
Example 2
Password: Code#1
The above password is a moderate password.
Example 3
Password: Name
This is a weak password.
Program: Check password strength in C++
See the example code for a better understanding.
#include <iostream>
using namespace std;
int main()
{
int l_case=0, u_case=0, digit=0, special=0;
string str;
cout<<"Enter input string."<<endl;
cin>>str;
int l=str.length(),i;
for(i=0;i<l;i++)
{
if(islower(str[i]))
l_case=1;
if(isupper(str[i]))
u_case=1;
if(isdigit(str[i]))
digit=1;
if(!isalpha(str[i]) && !isdigit(str[i]))
special=1;
}
if(l_case && u_case && digit && special && l>=8)
cout<<"Strong password."<<endl;
else if((l_case+u_case+digit+special>=3) && l>=6)
cout<<"Moderate password"<<endl;
else
cout<<"Weak password"<<endl;
return 0;
}The output of the program will be:
Enter input string. CodeSpeedy@1213 Strong password.
Enter input string. Code#1 Moderate password
Enter input string. Name Weak password
Leave a Reply