Password Verifier. Your task is to develop code that will verify the password th
ID: 650368 • Letter: P
Question
Password Verifier. Your task is to develop code that will verify the password the user is selecting meets the requirements of a password for the system. Create a C++ program which will verify that a password meets the following criteria:
The password should be at least 6 characters long.
The password should contain at least one uppercase and at least one lowercase letter.
The password should have at least one digit.
If the password does not meet these requirements, the program should display a message telling the user why the password is invalid, specifically. It should also continue to loop until the user enters a valid password.
Program must have the following functions (may also use string instead of a char array).
bool validatePassword(char [ ])
Explanation / Answer
C++ Program for Password Verifier :
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <cctype>
#include "ctype.h"
using namespace std;
bool lengthTest(char *);
bool lowCaseTest(char []);
bool upCaseTest(char []);
bool digitTest(char []);
const int SIZE = 20;
int main()
{
char password[SIZE];
int sumofbool;
cout << "----PASSWORD VERIFIER PROGRAM---- ";
cout << "Enter a password that meets the following criteria: "
<< "-Minimum of 6 characters in length. "
<< "-Contains at least one uppercase and one lowercase letter. "
<< "-Contains at least one digit. ";
cout << "->";
// Get input from user.
cin.getline(password, SIZE);
sumofbool = lengthTest(password) + lowCaseTest(password) + upCaseTest(password) + digitTest(password);
// if 1 or more of the 4 functions is not true, display Password is invalid and promt to re-enter.
while (sumofbool < 4)
{
if (!lengthTest(password))
cout << "Error, password must be at least 6 characters long. ";
if (!upCaseTest(password))
cout << "Error, password must contain at least one upper case letter. ";
if (!lowCaseTest(password))
cout << "Error, password must contain at least one lower case letter. ";
if (!digitTest(password))
cout << "Error, password must contain at least one digit. ";
cout << "Please re-enter password: ";
// prompt to re-enter and call functions to test input.
cin.getline(password, SIZE);
sumofbool = lengthTest(password) + lowCaseTest(password) + upCaseTest(password); + digitTest(password);
}
// if conditions for password are met, display message.
cout << " You have entered a valid password. ";
return 0;
}
//*********LENGTH TEST FUNCTION***********
bool lengthTest(char *str)
{
int numChar = 0;
bool validlength = false;
for (int cnt = 0; cnt < SIZE; cnt++)
{
while (*str != 0)
str++, numChar++;
}
if (numChar >= 6)
validlength = true;
return validlength;
}
//*********LOWERCASE LETTER TEST FUNCTION*********
bool lowCaseTest(char pass[])
{
for (int cnt = 0; cnt < SIZE; cnt++)
{
if (islower(pass[cnt]))
return true;
}
return false;
}
//********CAPITAL LETTER TEST FUNCTION*********
bool upCaseTest(char pass[])
{
for (int cnt = 0; cnt < 20; cnt++)
{
if (isupper(pass[cnt]))
return true;
}
return false;
}
//**********DIGIT TEST FUNCTION************
bool digitTest(char pass[])
{
for (int cnt = 0; cnt < 20; cnt++)
{
if (isdigit(pass[cnt]))
return true;
}
return false;
}