give a sample program, that will determine if a password entered by the user mee
ID: 3637397 • Letter: G
Question
give a sample program, that will determine if a password entered by the user meets the following criteria:The password entered is not the user's name. That is, ask the user to enter his/her name before asking for the password. Use the strcmp function from the cstring library to determine if the password is the user's name.
-should contain no fewer than 8 characters and no more than 12 characters.
-must contain at least one upper case letter and at least one lower case letter.
-must contain at least one digit.
-must contain at least one non-alphanumeric character.
-may not contain any spaces.
If the password meets the above criteria, a message should be displayed stating that the password is valid. Do not redisplay the password! If the password does not meet the above criteria, display a message listing what all the problems/deficiencies are. For example, if the user enters abcdef12, the messages should be similar to this:
Your password is invalid for the following reasons:
A valid password must contain at least one uppercase letter.
A valid password must contain at least one non-alphanumeric character.
The program should allow up to 3 attempts to enter a valid password. If, after 3 attempts, a valid password has not been entered, the program should state that the user must wait an hour before trying again.
the program should include the following:
The program must have at least 5 functions in addition to the main function.
The program must use char arrays, must use the "built-in" character-testing functions found in the cctype library, and must use the strlen and strcmp functions in the cstring library.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cctype>
using namespace std;
int checkchars(char[]);
int checkdigit(char[]);
int checkalpha(char[]);
int checkletters(char[]);
int checkblanks(char[]);
int main()
{char pw[20];
char name[20];
int a,b,c,d,e,count=0;
cout<<"Enter your name: ";
cin.getline(name,20);
do
{count++;
a=1;
cout<<"Enter a password: ";
cin.getline(pw,20);
if(strcmp(pw,name)==0)
cout<<"password cannot be your name ";
else
{
a=checkchars(pw);
b=checkdigit(pw);
c=checkalpha(pw);
d=checkletters(pw);
e=checkblanks(pw);
cout<<" ";
}
}while(a+b+c+d+e!=0&&count<3);
if(a+b+c+d==0)
cout<<pw<<" is a valid password ";
else
cout<<"You must wait an hour before trying again. ";
system("pause");
return 0;
}
int checkchars(char pw[])
{if(strlen(pw)<8||strlen(pw)>12)
{cout<<"must have bewteen 8 and 12 characters ";
return 1;
}
return 0;
}
int checkdigit(char pw[])
{int i,digit=0;
for(i=0;i<strlen(pw);i++)
if(isdigit(pw[i]) )
digit++;
if(digit==0)
{cout<<"must have at least 1 digit ";
return 1;
}
return 0;
}
int checkalpha(char pw[])
{int alpha=0,i;
for(i=0;i<strlen(pw);i++)
if(!isalnum(pw[i]))
alpha++;
if(alpha==0)
{cout<<"must have at least 1 non-alphabetic character ";
return 1;
}
return 0;
}
int checkblanks(char pw[])
{int alpha=0,i;
for(i=0;i<strlen(pw);i++)
if(pw[i]==' ')
alpha++;
if(alpha!=0)
{cout<<"may not contain any spaces ";
return 1;
}
return 0;
}
int checkletters(char pw[])
{int upper=0,lower=0,i;
for(i=0;i<strlen(pw);i++)
if(isupper(pw[i]))
upper++;
else if(islower(pw[i]))
lower++;
if(lower==0||upper==0)
{cout<<"must contain at least one upper case letter and at least one lower case letter. ";
return 1;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.