Password Validation Program in C++
Hello everyone!
In this tutorial, we will check out how to validate your password using C++ with the corresponding code. We will demonstrate to you how to check whether your password is a strong password or not. For that, we will check whether the password contains at least one uppercase character, one lower case character, and also at least one digit. In General, a strong should be of length more than 6 characters.
Password Validation Program in C++
Steps involved are:-
- The user provides the password which is to be check.
- The password provided by the user will now be check for uppercase, lowercase, and digits.
- The strongness of the password is classified into 3 categories which are, strong, moderate, and weak.
- If the password contains all of them then it is considered to be a Strong password.
- If the password contains at least an uppercase or lowercase character and at least a digit along with the length of 6 more, then it is considered as a Moderate one.
- else it is a weak password.
Below is the C++ code to validate your password:
#include <bits/stdc++.h>
using namespace std;
void checkpassword(string& password)
{
int n = password.length();
bool hasLower = false, hasUpper = false, hasDigit = false;
for (int i = 0; i < n; i++) {
if (islower(password[i]))
hasLower = true;
if (isupper(password[i]))
hasUpper = true;
if (isdigit(password[i]))
hasDigit = true;
}
// Displaying the strength of password
cout << "Strength of password you have entered is :-";
if ( hasUpper && hasDigit && hasLower && (n >= 6)) // considering a strong must be of length 6 or more
cout << "Strong" << endl;
else if ((hasLower || hasUpper) && hasDigit && (n >=6))
//when at least a lower case or uppercase is used along with digit
cout << "Moderate" << endl;
else
cout << "Weak" << endl;
}
int main()
{ cout << "Welcome, lets check your password " << endl;
cout << "Considering average length of a strong password should be more then 6 character " << endl;
cout << "Please enter a password which should contain :- " << endl;
cout << " * at least one upper and lowercase letter " << endl;
cout << " * and also at least one digit " << endl;
string password;
cout <<"Enter password"<<endl;
getline(cin,password);
checkpassword(password);
return 0;
}Outputs :
Case 1: For Strong password
Enter password Vivek123 Strength of password you have entered is:-Strong
Case 2: For Moderate password
Enter password vivek123 Strength of password you have entered is:-Moderate
Case 3: For Weak password
Enter password vivek Strength of password you have entered is:-Weak
Similarly, there can be many more inputs for which you can validate your password.
Give it a try.
I hope I have made my point clear. Feel free to ask any query.
You may also visit:
Thank You.
Leave a Reply