63.0k views
2 votes
Password Verifier Imagine you are developing a software package that requires users to enter their own passwords. Your software requires that users’ passwords meet the following criteria: The password should be at least six characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. Write a program that asks for a password then verifies that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why.

User Moxi
by
5.0k points

2 Answers

6 votes

Final answer:

To write a program that verifies if a password meets specific criteria, you can use a combination of string manipulation and conditional statements.

Step-by-step explanation:

To write a program that verifies if a password meets specific criteria, you can use a combination of string manipulation and conditional statements. Here is an example in Python:

password = input("Enter your password: ")
length = len(password)
has_uppercase = any(c.isupper() for c in password)
has_lowercase = any(c.islower() for c in password)
has_digit = any(c.isdigit() for c in password)

if length < 6:
print("Password should be at least six characters long.")
if not has_uppercase or not has_lowercase:
print("Password should have at least one uppercase and one lowercase letter.")
if not has_digit:
print("Password should have at least one digit.")
if length >= 6 and has_uppercase and has_lowercase and has_digit:
print("Password meets the criteria.")

This program prompts the user to enter their password, and then checks each criterion using a combination of the len() function, isupper(), islower(), and isdigit() methods. If any criterion is not met, an appropriate error message is displayed.

User Chris Fryer
by
6.1k points
6 votes

Answer:

Here is the C++ program:

#include <iostream> //to use input output functions

using namespace std; //to identify objects like cin cout

bool verifyLength(string password); // to verify length of password

bool verifyUpper(string password); //to verify if password contains uppercase letters

bool verifyLower(string password); //to verify if password has lowercase letters

bool verifyDigit(string password); // to verify if password contains digits

int main() { //start of main() function

string password = " "; //to hold password string input by user

while(true) { //loop that verifies the password. The loop keeps prompting user to enter a valid password telling what criteria is to be met to verify password.

cout << "\\Please enter your password: "; // prompts user to enter a password

getline(cin, password); //reads and gets password from user

if (verifyLength(password) == false) // calls verifyLength to check length criteria for password. Checks if length of password is less than 6

{ cout << "The password should be at least six characters long.\\";}

else if (verifyUpper(password) == false) { // calls verifyUpper to check criteria for password letters to be in uppercase. Checks if password does not have an uppercase letter

cout << "The password should contain at least one uppercase letter.\\"; }

else if (verifyLower(password) == false) { // calls verifyLower to check criteria for password letters to be in lowercase. Checks if password does not contain a lowercase letter

cout << "The password should contain at least one lowercase letter.\\"; }

else if (verifyDigit(password) == false) { // calls verifyDigit to check digit criteria for password. Checks if password does not contain a digit

cout << "The password should have at least one digit.\\"; }

else { //if password meets the criteria

cout << "Password verified!\\";

break; } } } //breaks the loop

Step-by-step explanation:

Here are the method that are called to verify the stated criteria:

bool verifyLength(string password) { // this method takes input password as parameter to verify the length criteria and returns true of false accordingly

bool length = false; // the bool type variable length is initially set to false

if(password.length() >= 6) //if the length of the password that is obtained by using length() function is greater than or equals to 6 then it means it meets valid password length criteria

length = true; //set length to true when above if statement is true

else //if length of password is less than 6

length = false; //set length to false if length of password is less than 6

return length; } //return the state of length

bool verifyUpper(string password) { // this method takes input password as parameter to verify the uppercase criteria and returns true of false accordingly

bool upper = false; // the bool type variable upper is initially set to false

for (int i = 0; i < password.length(); i++) { //the loop iterates through the letter of password string until the length of the string is reached

if (isupper(password[i])) { //it checks if any letter of the password is in uppercase using the isupper() method

upper = true; } } //set upper to true when above if condition is true

return upper; } //returns the state of upper

bool verifyLower(string password) { // this method takes input password as parameter to verify the lowercase criteria and returns true of false accordingly

bool lower = false; // the bool type variable lower is initially set to false

for (int i = 0; i < password.length(); i++) { //the loop iterates through the letter of password string until the length of the string is reached

if (islower(password[i])) { //it checks if any letter of the password is in lowercase using the islower() method

lower = true; } } //set lower to true when above if condition is true

return lower; } //returns the state of lower accordingly

bool verifyDigit(string password) { // this method takes input password as parameter to verify the digit criteria and returns true of false accordingly

bool digit = false; // the bool type variable digit is initially set to false

for (int i = 0; i < password.length(); i++) { //the loop iterates through each letter of password string until the length of the string is reached

if (isdigit(password[i])) { //it checks if any letter of the password is a digit using the isdigit() method

digit = true; } } //set digit to true when above if condition is true

return digit; } //returns the state of digit accordingly

The output of program is attached.

Password Verifier Imagine you are developing a software package that requires users-example-1
User Felix Haeberle
by
5.6k points