In: Computer Science
Imagine you are developing a software package that requires users to enter their own
passwords. Your software requires that user’s passwords meet the following criteria:
1. The password should be at least six characters long.
2. The password should contain at least one uppercase and at least one lowercase
letter.
3. The password should have at least one digit.
Write a program that asks for a password and then verifies that it meets the stated
criteria. If it doesn’t, the program should display a message telling the user why.
Your implementation should use a Class Password with a private data string type to
store the password, class methods as needed such as constructors, and public
methods that should be used in the main function to check the password:
bool isLongEnough();
bool hasDigit();
bool hasUpperAndLowerCase();
Needs to be in c++ and I am using Microsoft Visual Studio 2019. Thanks!
#include <iostream>
#include <string.h>
#include <list>
using namespace std;
//class password
class Password
{
string pwd;
public:
//default constructor
Password()
{
pwd = "";
}
//parameterize constructor
Password(string pass)
{
pwd = pass;
}
//function to check the length of the password
bool isLongEnough()
{
int length;
length = pwd.size();
if(length<6)
return false;
else
return true;
}
//function to check if the password has a digit
bool hasDigit()
{
for(int i=0; i<pwd.size(); i++)
{
if(isdigit(pwd[i]))
return true;
}
return false;
}
//function to check if the password has upper case and lower
case
bool hasUpperAndLowerCase()
{
int lowercase = 0, uppercase = 0;
for(int i=0; i<pwd.size(); i++)
{
if(isupper(pwd[i]))
uppercase++;
if(islower(pwd[i]))
lowercase++;
}
if(uppercase && lowercase)
return true;
return false;
}
};
int main()
{
//create object
Password password1("hHldfd#9");
Password password2("hHldfd");
//check the password
if(password1.isLongEnough() && password1.hasDigit()
&& password1.hasUpperAndLowerCase())
{
cout<<"Password format is correct.";
}
else
{
cout<<endl<<"Password format is not correct.";
}
if(password2.isLongEnough() && password2.hasDigit()
&& password2.hasUpperAndLowerCase())
{
cout<<endl<<"Password format is correct.";
}
else
{
cout<<endl<<"Password format is not correct.";
}
return 0;
}
OUTPUT: