Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Due ASAP, please respond if i comment about something wrong with the code. P

ID: 3753010 • Letter: C

Question

C++ Due ASAP, please respond if i comment about something wrong with the code. Please use lots of comments to explain the code. Write a password program that will ask the user to enter a password and it will check to make sure it is 1. A combination of letters and numbers 2. it has at least 2 special characters 3. it is at least 16 characters in length 4. it includes no vowels in the password 5.Must have at least one upper case and one lower case letter. If any of these are wrong it should tell the user exactly what was wrong and let them try a new password. Additonally there should be the option for the user to get a randomly generated password(this will follow the same criteria. Thank You for your help.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

//helper method to check if a character is a vowel

bool isVowel(char c){

              //the below switch block will return true if character is

              // a/e/i/o/u or A/E/I/O/U

              switch(c){

                             case 'A': //vowel

                             case 'E'://vowel

                             case 'I'://vowel

                             case 'O'://vowel

                             case 'U'://vowel

                             case 'a'://vowel

                             case 'e'://vowel

                             case 'i'://vowel

                             case 'o'://vowel

                             case 'u': return true;//vowel

                             default: return false; //not a vowel

              }

}

//helper method to check if a character is a letter

bool isLetter(char c){

              if((c>='A' && c<='Z') || (c>='a' && c<='z') ){

                             return true;

              }

              return false;

}

//helper method to check if a character is a digit

bool isDigit(char c){

              if(c>='0' && c<='9'){

                             return true;

              }

              return false;

}

//helper method to check if a character is an upper case letter

bool isUpperCase(char c){

              if(c>='A' && c<='Z'){

                             return true;

              }

              return false;

}

//helper method to check if a character is a lower case letter

bool isLowerCase(char c){

              if(c>='a' && c<='z'){

                             return true;

              }

              return false;

}

//helper method to check if a character is a special character

//here, any character other than letters and digits are considered as

//a special character

bool isSpecialChar(char c){

              if(!isLetter(c) && !isDigit(c)){

                             return true;

              }

              return false;

}

// method to check if a password is valid.

//returns "Password is valid" if it is valid, otherwise, return the reason why it is invalid

string checkPassword(string password){

              //finding length

              int length=password.length();

              if(length<16){

                             //insufficient length

                             return "Password must contain atleast 16 characters";

              }

              //defining variables to store counts of each character types

              int numLetters=0, numUpperCase=0, numLowerCase=0, numSpecialChars=0;

              int numVowels=0, numDigits=0;

              for(int i=0;i<length;i++){

                             //finding the character, incrementing the specified counter

                             char c=password[i];

                             if(isLetter(c)){

                                           numLetters++;

                             }

                             if(isUpperCase(c)){

                                           numUpperCase++;

                             }

                             if(isLowerCase(c)){

                                           numLowerCase++;

                             }

                             if(isVowel(c)){

                                           numVowels++;

                             }

                             if(isSpecialChar(c)){

                                           numSpecialChars++;

                             }

                             if(isDigit(c)){

                                           numDigits++;

                             }

              }

              if(numLetters==0 || numDigits==0){

                             //either number of letters is 0 or number of digits is 0

                             return "Password must be a combination of letters and digits!";

              }

              if(numSpecialChars<2){

                             //two special characters are needed

                             return "Password must contain atleast two special characters!";

              }

              if(numVowels>0){

                             //uh oh! no vowels allowed

                             return "Password must not contain a vowel!";

              }

              if(numUpperCase==0 || numLowerCase==0){

                             //should contain atleast upper case and one lower case chars

                             return "Password must contain atleast one upper case and one lower case character!";

              }

              //all checks passed, password is valid

              return "Password is valid";

}

//method to generate a random password

string generatePassword(){

              string pass=""; //output password

              //defining strings for each character set

              string upper="BCDFGHJKLMNPQRSTVWXYZ"; //upper case char set with no vowels

              string lower="bcdfghjklmnpqrstvwxyz";//lower case char set with no vowels

              string digits="0123456789"; //set of digits

              string special="?.@#$%&*;:"; //set of special chars

              int index=0;

              //filling first 8 positions with random lower case characters

              for(int i=0;i<8;i++){

                             //generating random index between 0 and length of lower string -1

                             index=rand()%lower.length();

                             //adding character at this index to password

                             pass+=lower[index];

              }

              //filling next 2 positions with random digits

              for(int i=0;i<2;i++){

                             index=rand()%digits.length();

                             pass+=digits[index];

              }

              //filling next 4 positions with random upper case characters

              for(int i=0;i<4;i++){

                             index=rand()%upper.length();

                             pass+=upper[index];

              }

              //filling last 2 positions with random special characters

              for(int i=0;i<2;i++){

                             index=rand()%special.length();

                             pass+=special[index];

              }

              return pass;

}

int main(){

              //seeding random number generator

              srand(time(NULL));

              string pass;

              char choice=' ';

              //loop until user quits

              while(choice!='q'){

                             //displaying a menu

                             cout<<"a. Check if a password is valid"<<endl;

                             cout<<"b. Generate a random password"<<endl;

                             cout<<"q. Quit"<<endl;

                             cout<<"# Enter your choice: ";

                             cin>>choice;

                             switch(choice){

                                           case 'a':

                                                          //prompting and checking if a password is valid

                                                          cout<<"Enter password to check: ";

                                                          cin>>pass;

                                                          cout<<checkPassword(pass)<<endl;

                                                          break;

                                           case 'b':

                                                          //generating and displaying a random password

                                                          pass=generatePassword();

                                                          cout<<"Generated password: "<<pass<<endl;

                                                          break;

                             }

              }

              return 0;

}

/*OUTPUT*/

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: sometext

Password must contain atleast 16 characters

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: somelonglongrandomtext

Password must be a combination of letters and digits!

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: somelonglongrandomtext123

Password must contain atleast two special characters!

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: somelonglongrandomtext?;123

Password must not contain a vowel!

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: smlnglngrndmtxt123?:@@

Password must contain atleast one upper case and one lower case character!

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: a

Enter password to check: Smlnglngrndmtxt123?:@@

Password is valid

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: b

Generated password: vrklhkyc15TLRK$*

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: b

Generated password: gqgprbyj17TQNN@*

a. Check if a password is valid

b. Generate a random password

q. Quit

# Enter your choice: q