1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <iomanip>
using namespace std;
const int NUM_STRINGS = 7; // Global named constant: size of testString array
const int MIN_LENGTH = 8; // Global named constant: Min length of password
const int MAX_LENGTH = 10; // Global named constant: Max length of password
bool isValidPassword(string pwd, char spec[], int siz);
int main()
{
string testString[NUM_STRINGS] = {"12A4&z78", "12A4&z78(", "12A45z78","12A4$Z78", "a124$z78", "12A4&z7", "12A4&z7890T" };
char special[] = {'#', '$', '%', '&', '!', '?', '@'};
int numSpecial = sizeof(special)/sizeof(char); // Calculate the size of special array
// Driver for isValidPassword()
for (int i = 0; i < NUM_STRINGS; i ++)
cout << setw(14) << testString[i] << (isValidPassword(testString[i], special, numSpecial)? " is valid": " is not valid") << endl;
return 0;
}
bool isValidPassword(string pwd, char spec[], int siz)
{
bool hasDigit = false;
bool hasLower = false;
bool hasUpper = false;
bool hasSpecial = false;
bool hasIllegal = false;
for(int i = 0; i < pwd.length(); i++)
{
if(isdigit(pwd.at(i)))
hasDigit = true;
}
for(int i = 0; i < pwd.length(); i++)
{
if(islower(pwd.at(i)))
hasLower = true;
}
for(int i = 0; i < pwd.length(); i++)
{
if(isupper(pwd.at(i)))
hasUpper = true;
}
for(int i = 0; i < pwd.length(); i++)
{
for(int x = 0; x < siz; x++)
if(pwd.at(i) == spec[x])
hasSpecial = true;
}
for(int i = 0; i < pwd.length(); i++)
{
if(!isdigit(pwd.at(i)))
{
if(!islower(pwd.at(i)))
{
if(!isupper(pwd.at(i)))
{
if(pwd.at(i) != '#' && pwd.at(i) != '$' && pwd.at(i) != '%' && pwd.at(i) != '&' && pwd.at(i) != '!' && pwd.at(i) != '?' && pwd.at(i) != '@' )
{
hasIllegal = true;
}
}
}
}
}
if (pwd.length() < MIN_LENGTH || pwd.length() > MAX_LENGTH)
{
return false;
}
else if (hasDigit == true && hasLower == true && hasUpper == true && hasSpecial == true && hasIllegal == false)
return true;
}
|