//Sam Partovi CS1A Ch. 10, P.590, #12
/*******************************************************************************
* DETERMINE PASSWORD VALIDITY
* ____________________________________________________________
* This program accepts a given password and determines its validity based on
* specified minimum password requirements, such as character and digit count.
* ____________________________________________________________
*INPUT
* userPassword : Given user password
* minLength : Minimum length of password
* minUppercase : Minimum number of uppercase letters
* minLowercase : Minimum number of lowercase letters
* minDigits : Minimum number of digits
*OUTPUT
* Messages indicating whether the password meets the requirements or not.
*******************************************************************************/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
//Structure to define password requirements
struct Password {
int minLength; //INPUT - Minimum length of password
int minUppercase; //INPUT - Minimum number of uppercase letters
int minLowercase; //INPUT - Minimum number of lowercase letters
int minDigits; //INPUT - Minimum number of digits
};
//Function prototype
void PasswordVerifyFunc(const string& userPassword,
const Password& requirements);
int main() {
//Password requirements
Password requirements = {6, 1, 1, 1};
string userPassword;
//Prompt for a password
cout << "Enter a password: ";
cin >> userPassword;
cout << endl;
//Verify the password
PasswordVerifyFunc(userPassword, requirements);
return 0;
}
/*******************************************************************************
* Definition of PasswordVerifyFunc function:
* This function verifies the given password in userPassword against the
* specified requirements in the Password structure.
*******************************************************************************/
void PasswordVerifyFunc(const string& userPassword,
const Password& requirements) {
int charCount = userPassword.length(); //Get the length of the password
int uppercaseCount = 0; //Count uppercase letters
int lowercaseCount = 0; //Count lowercase letters
int digitCount = 0; //Count digits
//Iterate through the password to count character types
for (char c : userPassword) {
if (isupper(c)) uppercaseCount++;
if (islower(c)) lowercaseCount++;
if (isdigit(c)) digitCount++;
}
//Check requirements and display results
bool isValid = true;
if (charCount < requirements.minLength) {
cout << "Password must be at least " << requirements.minLength
<< " characters long.\n";
isValid = false;
}
if (uppercaseCount < requirements.minUppercase) {
cout << "Password must contain at least " << requirements.minUppercase
<< " uppercase letter(s).\n";
isValid = false;
}
if (lowercaseCount < requirements.minLowercase) {
cout << "Password must contain at least " << requirements.minLowercase
<< " lowercase letter(s).\n";
isValid = false;
}
if (digitCount < requirements.minDigits) {
cout << "Password must contain at least " << requirements.minDigits
<< " digit(s).\n";
isValid = false;
}
//If all requirements are met
if (isValid) {
cout << "Password is valid.\n";
}
}