In: Computer Science
Please make it simply and easy for a beginner to follow
-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 characte
- In case any problem please comment, I am happy to assist you.
Program :-
#include <bits/stdc++.h>
using namespace std;
void CheckPassword(string& password)
{
int n = password.length(), flag=0;
bool hasLower = false, hasUpper = false, hasDigit = false;
for (int i = 0; i < n; i++) {
if (islower(password[i]))
hasLower = true;
if (isupper(password[i]))
hasUpper = true;
if (isdigit(password[i]))
hasDigit = true;
}
for(int i=0;i<n;i++)
{
if ((password[i]>=48 && password[i]<=57)||
(password[i]>=65 && password[i]<=90)||
(password[i]>=97 && password[i]<=122))
{
continue;
}
else
{
cout<<"String contains special character.\n";
flag=1;
break;
}
}
if(( hasLower && hasUpper && hasDigit &&
(n>=8)) && (flag==1) )
cout<< "Valid password";
else
cout<<"Invalid password";
}
int main()
{
string password;
cout <<"Enter your password"<<endl;
getline(cin,password);
CheckPassword(password);
return 0;
}