In: Computer Science
c++
Develop a program that validates a password entered by a user. The password must meet the following criteria:
Write a program that asks for a password and then verifies that it meets the stated criteria. If it does not, the program should display a message telling the user why and ask for another password. The program should keep asking for the password until the password meets the criteria.
The validation should be done on a separate function called validation, which takes a string variable as parameter and returns an integer. It should return 0 if the password is valid and 1 if it does not match the first criteria, 2 if it does not match the second criteria and 3 if it does not match the third criteria.
Code:
#include <iostream>
//as we are using isupper() ,islower(),and isdigit()
//functions, we have to include this library
#include <cctype>
using namespace std;
//function named validation which takes a string named
//pass as argument and returns a int is defined
int validation(string pass){
//length of pass
int len=pass.length();
//variables to store counts of uppercase,
//lower case, and digits
int upper_count=0;
int lower_count=0;
int digit_count=0;
//traversing the string and checking each
character
//and incrementing the respective count variable
for(int i=0;i<len;i++){
if(isupper(pass[i])){
upper_count++;
}
if(islower(pass[i])){
lower_count++;
}
if(isdigit(pass[i])){
digit_count++;
}
}
//if len is lessthan 6 returning 1
if(len<6){
return 1;
}
//if either of uppercount and lowercount are lessthan
1
//returning 2
else if(upper_count<1 || lower_count<1){
return 2;
}
//if digit_count is lessthan 1 returning 3
else if(digit_count<1){
return 3;
}
//if all of the above cases are false then return
0
else{
return 0;
}
}
//main functions
int main(){
//a string named password
string password;
//this loop iterates until a valid password is
entered
while(true){
//taking input
cout<<"Enter password: "<<endl;
cin>>password;
//calling function validation with argument
password
//and storing the return value in res
int res = validation(password);
//showing the reason
if(res==0){
cout<<"password is
valid"<<endl;
break;
}
else if(res==1){
cout<<"Password should be
atleast six characters"<<endl;
}
else if(res==2){
cout<<"Password should
contain atleast 1 uppercase and 1 lowercase
letters"<<endl;
}
else if(res==3){
cout<<"Passsword should
contain atleast 1 digit"<<endl;
}
else{
cout<<"Invalid"<<endl;
}
}
}
Output:
Code Screenshot:
Code Snippet:
#include <iostream>
//as we are using isupper() ,islower(),and isdigit()
//functions, we have to include this library
#include <cctype>
using namespace std;
//function named validation which takes a string named
//pass as argument and returns a int is defined
int validation(string pass){
//length of pass
int len=pass.length();
//variables to store counts of uppercase,
//lower case, and digits
int upper_count=0;
int lower_count=0;
int digit_count=0;
//traversing the string and checking each character
//and incrementing the respective count variable
for(int i=0;i<len;i++){
if(isupper(pass[i])){
upper_count++;
}
if(islower(pass[i])){
lower_count++;
}
if(isdigit(pass[i])){
digit_count++;
}
}
//if len is lessthan 6 returning 1
if(len<6){
return 1;
}
//if either of uppercount and lowercount are lessthan 1
//returning 2
else if(upper_count<1 || lower_count<1){
return 2;
}
//if digit_count is lessthan 1 returning 3
else if(digit_count<1){
return 3;
}
//if all of the above cases are false then return 0
else{
return 0;
}
}
//main functions
int main(){
//a string named password
string password;
//this loop iterates until a valid password is entered
while(true){
//taking input
cout<<"Enter password: "<<endl;
cin>>password;
//calling function validation with argument password
//and storing the return value in res
int res = validation(password);
//showing the reason
if(res==0){
cout<<"password is valid"<<endl;
break;
}
else if(res==1){
cout<<"Password should be atleast six characters"<<endl;
}
else if(res==2){
cout<<"Password should contain atleast 1 uppercase and 1 lowercase letters"<<endl;
}
else if(res==3){
cout<<"Passsword should contain atleast 1 digit"<<endl;
}
else{
cout<<"Invalid"<<endl;
}
}
}