In: Computer Science
-Write in C++
-Use Char library functions
Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties:
1. At least 8 characters long
2. Has at least one upper case letter
3. Has at least one lower case letter
4. Has at least one digit
5. Has at least on special character
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
// function declarations
bool isValidPassword(string password);
int main()
{
// Declaring variables
string password;
int cnt;
/* This while loop continues to execute
* until the user enters a valid password
*/
while (true)
{
cnt = 0;
// Getting the input entered by the user
cout << "\nEnter password :";
cin >> password;
// calling the function
bool b = isValidPassword(password);
if (b)
{
cout << "Valid Password" << endl;
break;
}
else
{
cout << "Invalid Password" << endl;
}
}
return 0;
}
bool isValidPassword(string password)
{
int flag = 0, cnt = 0;
if (password.length() < 8)
{
cout << "Length of the password must be atleast 8" <<
endl;
cnt++;
}
/* This function check whether the password
* contains atleast 1 lowercase character or not
*/
for (int i = 0; i < password.length(); i++)
{
if (isupper(password[i]))
{
flag = 1;
}
}
if (flag == 0)
{
cnt++;
cout << "Password contains atleast 1 lowercase character"
<< endl;
}
flag = 0;
/* Checking whether the password
* contains atleast 1 uppercase character or not
*/
for (int i = 0; i < password.length(); i++)
{
if (isupper(password[i]))
{
flag = 1;
}
}
if (flag == 0)
{
cnt++;
cout << "Password contains atleast 1 uppercase character"
<< endl;
}
flag = 0;
/* checking whether the password
* contains atleast 1 digit or not
*/
for (int i = 0; i < password.length(); i++)
{
if (isdigit(password[i]))
{
flag = 1;
}
}
if (flag == 0)
{
cnt++;
cout << "Password contains atleast 1 digit" <<
endl;
}
flag = 0;
/* checking whether the password
* contains atleast 1 special character or not
*/
for (int i = 0; i < password.length(); i++)
{
if ((password[i] == '!') || (password[i] == '@') || (password[i] ==
'#')
|| (password[i] == '$'))
{
flag = 1;
}
}
if (flag == 0)
{
cnt++;
cout << "Password must contain atleast one special character"
<< endl;
}
if (cnt == 0)
return true;
else
return false;
}
=======================================
=========================================
Output:
=====================Could you plz rate me
well.Thank You