In: Computer Science
Please make it simply and easy for a beginner to follow..
-Write in C++
-Use Char library functions
-Must show that is runs
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
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
bool isValidPassword(char str[])
{
int len = strlen(str);
if(len<8)
return false;
int upper=0,lower=0,digit=0,special=0;
for(int i=0;i<len;i++)
{
if(isupper(str[i]))
upper++;
else if(islower(str[i]))
lower++;
else if(isdigit(str[i]))
digit++;
else
special++;
}
if(upper<1||lower<1||digit<1||special<1)
return false;
return true;
}
int main()
{
char str1[9]="Ab1@adf";
char str2[9]="Ab1@Bc2$";
char str3[9]="ab1@adfh";
char str4[9]="AB1@ADFH";
char str5[9]="Abb@adfh";
char str6[9]="Ab11adfh";
cout<<isValidPassword(str1)<<endl;
cout<<isValidPassword(str2)<<endl;
cout<<isValidPassword(str3)<<endl;
cout<<isValidPassword(str4)<<endl;
cout<<isValidPassword(str5)<<endl;
cout<<isValidPassword(str6)<<endl;
return 0;
}
//Program with input from user
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
bool isValidPassword(char str[])
{
int len = strlen(str);
if(len<8)
return false;
int upper=0,lower=0,digit=0,special=0;
for(int i=0;i<len;i++)
{
if(isupper(str[i]))
upper++;
else if(islower(str[i]))
lower++;
else if(isdigit(str[i]))
digit++;
else
special++;
}
if(upper<1||lower<1||digit<1||special<1)
return false;
return true;
}
int main()
{
char str[100];
cout<<"Enter the password: ";
cin>>str;
if(isValidPassword(str))
cout<<"Valid";
else
cout<<"InValid";
return 0;
}