Write a program in C++ that will check a new password and make sure it’s valid.
ID: 3676627 • Letter: W
Question
Write a program in C++ that will check a new password and make sure it’s valid. The program will start by asking the user to enter his/her full name (the full name must be stored in one string variable), and then his/her login ID (also a string variable). Then the program asks the user to enter a new password and validates it.
Here is the criteria for valid passwords:
-Must contain at least 8 characters
-Must contain at least one character from three of the following four categories:
Uppercase letters (A – Z), lowercase letters (a – z), numbers (digits 0 – 9), non-alphabetic characters (for example: @ # $ % ^ & * - + = | { ] ? / etc.)
-Must not contain any of following:
The user’s login ID, the user’s first or last name, or the user’s initials (defined as the first name initial followed by the last name initial)
If the password entered by the user meets the all criteria the program lets the user know that the password has been changed. If the password entered does not meet at least one of the criteria, the program issues an error message and ask the user to try again (as many time as needed unti lthe user provides a valid password).
Explanation / Answer
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
bool validatePassword(char password[]);
int main()
{
const int SIZE = 9;
char password[SIZE];
char username[20],userid[20];
char fname[15],lname[10];
cout << " Plase Enter your full name " << endl;
cin>>username;
cout << " Plase Enter User id" << endl;
cin>>userid;
cout << " Welcome to the Password validation code and rules as below" << endl;
cout << " Password contain at least 8 characters " << endl;
cout << " Uppercase letters (A – Z), non-alphabetic characters " << endl;
cout << " lowercase letters (a – z)" << endl;
cout << " numbers (digits 0 – 9) " << endl;
cin >> password;
if (strlen(password) < 8)
{
cout << "Password not long enough, please try again";
cin >> password;
}
if (validatePassword(password))
{
cout << "That's a valid password";
}
return 0;
}
bool validatePassword(char password[])
{
int count;
int upper=0,lower=0,digit=0,sc=0;
int u=1,l=1,d=1,c=1;
for (count = 0; count < 6; count++)
{
if (isupper(password[count]))
{
upper++;
}
elseIf (islower(password[count]))
{
lower++;
}
elseIf (isdigit(password[count]))
{
digit++;
}
else
{
sc++;
}
if (upper == 0)
{
cout << "no upper case, please try again ";
cin>>password;
u=0;
}
elseIf(lower==0)
{
cout << "no lower case, please try again ";
cin>>password;
l=0;
}
elseIf(digit == 0)
{
cout << "no digits , please try again ";
cin>>password;
d=0;
}
else
{
if(sc==0)
s=0;
}
int sum=u+l+d+s;//checking 4 of 3 conditions satisfy or not
int flag=0;
/*Checking whether password have The user’s login ID, the user’s first or last name, or the user’s initials*/
int pos = input_str.find_first_of(' ');//sperating fname,lname
std::string fname = input_str.substr(pos+1),lname = input_str.substr(0, pos);
if(password!=userid &&password!=fname && password!=lname)
{
flag=1;
}
If(sum>=3 && flag=0)
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.