Write a function boolean isValidPassword (String password)
A Password is valid if it satisfies following conditions:
1. Length of the password must be more than 10(without counting spaces).
2. Password must contain at least one capital and one small alphabet
3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition.
4. Password must contain at least one special character from the given set-
{$, @, _, -, ., /}
1
Expert's answer
2015-10-13T02:56:23-0400
Solve #include <iostream> #include <string>
using namespace std;
bool isValidPassword(string pass) { //1. Length of the password must be more than 10(without counting spaces) int i,k=0; int kCapital=0,kSmall=0,kDigit=0,kSpec=0; for(i=0;i<pass.length();i++) { if(pass[i]!=' ') k++; if((pass[i]>='a')&&(pass[i]<='z')) kSmall++; if((pass[i]>='A')&&(pass[i]<='Z')) kCapital++; if((pass[i]>='0')&&(pass[i]<='9')) kDigit++; //$, @, _, -, ., / if((pass[i]=='$')||(pass[i]=='@')||(pass[i]=='_')||(pass[i]=='-') ||(pass[i]=='.')||(pass[i]=='/')) kSpec++; } if(k<=10) return false; //2. Password must contain at least one capital and one small alphabet if((kCapital==0)||(kSmall==0)) return false; //3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition. if(kDigit<2) if(kSpec==0) return false; return true; }
Comments
Leave a comment