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.

Answer :

mahamnasir

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 << "\nPlease 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.\n";}                                        

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.\n";  }  

         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.\n";   }

          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.\n";    }  

        else {  //if password meets the criteria

          cout << "Password verified!\n";  

         break;   }     } }   //breaks the loop                

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.

${teks-lihat-gambar} mahamnasir

Other Questions