// Diego Martinez CSC5 Chapter 10, P.590, #12
/*******************************************************************************
* VALIDATE PASSWORD
* ______________________________________________________________________________
* This program asks the user to enter a password and verifies that it meets
* the required criteria:
*
* - At least 6 characters long
* - Contains at least one uppercase letter
* - Contains at least one lowercase letter
* - Contains at least one digit
*
* The program displays messages explaining why the password is invalid if
* it does not meet the requirements.
*
* ______________________________________________________________________________
* INPUT
*
* User password
*
* OUTPUT
*
* Validation results for the password
*
*******************************************************************************/
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
// Function prototype
bool verifyPassword(const char *);
int main()
{
// Variable declaration
char password[100];
// User input
cout << "Enter a password: ";
cin.getline(password, 100);
// Verify password
if (verifyPassword(password))
{
cout << "Password is valid." << endl;
}
else
{
cout << "Password is invalid." << endl;
}
return 0;
}
// Function definition
bool verifyPassword(const char *str)
{
int length = strlen(str);
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
// Check length
if (length < 6)
{
cout << "Password must be at least 6 characters long." << endl;
}
// Check characters
for (int i = 0; i < length; i++)
{
if (isupper(*(str + i)))
{
hasUpper = true;
}
if (islower(*(str + i)))
{
hasLower = true;
}
if (isdigit(*(str + i)))
{
hasDigit = true;
}
}
// Display missing requirements
if (!hasUpper)
{
cout << "Password must contain at least one uppercase letter." << endl;
}
if (!hasLower)
{
cout << "Password must contain at least one lowercase letter." << endl;
}
if (!hasDigit)
{
cout << "Password must contain at least one digit." << endl;
}
// Return result
if (length >= 6 && hasUpper && hasLower && hasDigit)
{
return true;
}
else
{
return false;
}
}