In: Computer Science
Using C++
Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space.
Functions:
You will need at least the following functions, please add more if needed. Your main function shouldn’t be more than half a page. Notice that most of the functions does not have “size” as an input parameter (why?).
You should have the following variables in main:
int size = 80;
char * ptrPassword;
Function header |
Purpose |
char * getInput(const int SIZE) |
The function takes user input and save it into the “password” input parameter |
bool checkLength(char * password) |
The password must be at least six characters long. And no more than 12 characters long |
bool checkCase(char * password) |
The password must contain at least one uppercase and at least one lower case letter. |
bool checkDigit(char * password) |
The password must contain at least one digit. |
bool checkSymbol(char * password) |
The password must contain at least one symbol that is not “alphanumeric”. |
bool checkWhiteSpace(char * password) |
The password may NOT contain any whitespace characters. |
The program should ask for a password and then verify that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why it fails, and then ask for re-entry until it is correct.
Your program should echo the correct password (shown in the sample output)
Sample Output:
Please enter a password: 123 !!!Error!!! The password is too short. !!!Error!!! The password should contain at least one upper case !!!Error!!! The password should contain at least one lower case !!!Error!!! The password should contain at least one symbol password !!!Error!!! The password should contain at least one upper case !!!Error!!! The password should contain at least one digit !!!Error!!! The password should contain at least one symbol Please enter a password: Password !!!Error!!! The password should contain at least one digit !!!Error!!! The password should contain at least one symbol Please enter a password: Password1 !!!Error!!! The password should contain at least one symbol Please enter a password: Password1 # !!!Error!!! The password cannot contain a space Please enter a password: Password1234$ !!!Correct!!! The password you’ve entered is: Password1234$ |
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
char * getInput(const int SIZE);
bool checkLength(char * password);
bool checkCase(char * password);
bool checkDigit(char * password);
bool checkSymbol(char * password);
bool checkWhiteSpace(char * password);
int main()
{
int size = 80;
char *ptrPassword;
ptrPassword = getInput(size);
bool isCorrect = true;
if(!checkLength(ptrPassword))
isCorrect = false;
if(!checkCase(ptrPassword))
isCorrect = false;
if(!checkDigit(ptrPassword))
isCorrect = false;
if(!checkSymbol(ptrPassword))
isCorrect = false;
if(!checkWhiteSpace(ptrPassword))
isCorrect = false;
if(isCorrect){
cout
<<"!!!Correct!!! The password you’ve entered is: ";
int size =
strlen(ptrPassword);
int i=0;
for(i=0;i<size;i++)
cout <<ptrPassword[i];
}
return 0;
}
char * getInput(const int SIZE){
char *password;
password = new char[SIZE];
cout << "Please enter a password:"
<< endl;
cin >> password;
return password;
}
bool checkLength(char * password){
int size =strlen(password);
if(size<7){
cout
<<"!!!Error!!! The password is too short."<<endl;
return false;
}
else if(size>13){
cout
<<"!!!Error!!! The password is too long."<<endl;
return false;
}
else
return true;
}
bool checkCase(char * password)
{
int count = 0; //loop counter
int size = strlen(password);
int lcount = 0;
int ucount = 0;
while(count<size){
if(islower(password[count]))
lcount++;
if(isupper(password[count]))
ucount++;
count++;
}
if(lcount==0){
cout
<<"!!!Error!!! The password should contain at least one lower
case"<<endl;
}
if(ucount==0){
cout
<<"!!!Error!!! The password should contain at least one upper
case"<<endl;
}
if(lcount==0||ucount==0)
return false;
return true;
}
bool checkDigit(char * password){
int count = 0; //loop counter
int size = strlen(password);
while(count<size){
if(isdigit(password[count]))
return true;
count++;
}
cout <<"!!!Error!!! The password should contain
at least one digit"<<endl;
return false;
}
bool checkSymbol(char * password){
int count = 0; //loop counter
int size = strlen(password);
while(count<size){
if(ispunct(password[count]))
return true;
count++;
}
cout <<"!!!Error!!! The password should contain
at least one symbol"<<endl;
return false;
}
bool checkWhiteSpace(char * password){
int count = 0; //loop counter
int size = strlen(password);
while(count<size){
if(isspace(password[count])){
cout <<"!!!Error!!! The password cannot contain a
space"<<endl;
return false;
}
count++;
}
return true;
}