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

A password requirements for creating an account at a certain company are the fol

ID: 3837444 • Letter: A

Question

A password requirements for creating an account at a certain company are the following
-At least 8 characters (max of 14)
-One uppercase letter
-One lowercase letter
-One number 0-9
-One special character(*,&,%, etc)

Create a C++ program USING FUNCTIONS that.
-Checks if the password meets the requirements.
-Uses filestreams to check for several passwords
-If the password meets the requirements:

-Prints the count of how many passwords there are
-Prints the average number of characters for the passwords
-Prints how many characters are in each password.
Assumptions:

The input file will not be empty.

USE isalpha(ch) – return true if ch is a letter, false otherwise isdigit(ch) – return true if ch is a digit, false otherwise isupper(ch) – return true if ch is an uppercase letter, false otherwise islower(ch) – return true if ch is a lowercase letter, false otherwise.

Do not use any other header files.
Start of the code:

#include <iostream>
#include <fstream>
#include <string>

int main()
{

return 0;
}

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

// to check if the string contains upper case letter or not
bool checkUpper(string pass){

int i;
for(i=0;i<pass.size();i++){
if(isupper(pass[i])) // to check for upper case letter
return true; // returns true if upper case letter is found
}
return false; // returns false if no upper case letter is found
}

// to check if the string contains lower case letter or not
bool checkLower(string pass){

int i;
for(i=0;i<pass.size();i++){
if(islower(pass[i])) // to check for lower case letter
return true; // returns true if lower case letter is found
}
return false; // returns false if no lower case letter is found
}

// to check if the string contains digit or not
bool checkDigit(string pass){

int i;
for(i=0;i<pass.size();i++){
if(isdigit(pass[i])) // to check for digits
return true; // returns true if digit is found
}
return false; // returns false if no digit is found
}

// to check if the string contains special character or not
// a character which is not an alphabet or a digit is a special character
bool checkSpecialCharacter(string pass){

int i;
for(i=0;i<pass.size();i++){
if(!isdigit(pass[i])&&!isalpha(pass[i])) // to check for special characters
return true; // returns true if special character is found
}
return false; // returns false if no special character is found
}

bool validate(string pass){

if(pass.size()>=8&&pass.size()<=14
&&checkUpper(pass)
&&checkLower(pass)
&&checkDigit(pass)
&&checkSpecialCharacter(pass)) // password is validated if all the requirements are met
return true;
else
return false;

}

int main(){


fstream fp;
string pass; // to store the password
int valid_password;//to store the number of valid passwords
int no_of_characters=0;//to store the no. of characters in each valid password
int sum=0;//to store total no. of characters in valid passwords
int no_of_valid_passwords=0;
fp.open("C:\Users\Dell\Desktop\password.txt");// change the file name accordingly

char output[100];
if(fp.is_open()){

while(!fp.eof()){

getline(fp, pass);// to read the passwords line by line

// if password is valid
if(validate(pass)){

no_of_valid_passwords++;// to increment the password count
no_of_characters=pass.size();
sum+=no_of_characters;

cout<<pass<<" is a valid password"<<endl;
cout<<"No. of characters are "<<no_of_characters<<endl;
}
}

cout<<"Total no. of valid passwords are "<<no_of_valid_passwords<<endl;
if(no_of_valid_passwords>0){
float avg=(sum/no_of_valid_passwords);//to get the average no. of characters;


cout<<"Average no. of characters are "<<avg<<endl;
}

}
fp.close();
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote