1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- /* Create a function that determines the minimum number of characters needed to make a strong password.
- * A password is considered strong if it satisfies the following criteria:
- * Its length is at least 6.
- * It contains at least one digit.
- * It contains at least one lowercase English character.
- * It contains at least one uppercase English character.
- * It contains at least one special character: !@#$%^&*()-+
- * Types of characters in a form you can paste into your solution:
- */
- bool isSpecial(char c){
- return c == '!' || c == '@' || c == '#' || c == '$' ||\
- c == '%' || c == '^' || c == '&' || c == '*' ||\
- c == '(' || c == ')' || c == '-' || c == '+';
- }
- int strongPassword(std::string password) {
- bool hasDigit = false;
- bool hasLower = false;
- bool minLength = password.size()>=6;
- bool hasUpper = false;
- bool hasSpecial = false;
- for(int i=0;i<password.size();i++){
- if(isdigit(password[i])){hasDigit=true;}
- if(islower(password[i])){hasLower=true;}
- if(isupper(password[i])){hasUpper=true;}
- if(isSpecial(password[i])){hasSpecial=true;}
- }
- // return hasDigit && hasLower && minLength && hasUpper && hasSpecial;
- // return !hasDigit + !hasLower + !minLength + !hasUpper + !hasSpecial;
- // number of criteria have matched
- int criteriaMatch = (hasDigit+hasLower+hasUpper+hasSpecial);
- // number of missing criteria
- int missingCriteria =(!hasDigit+!hasLower+!hasUpper+!hasSpecial);
- // the number of non-contributing characters
- int fluff = password.size() - criteriaMatch;
- // if the number of missing criteria would not add up to six characters
- // we need to add, additional characters.
- int minKneaded = 6 - fluff;
- // int total = fluff + missingCriteria;
- // if(total<6){
- // return missingCriteria + 6-total;
- // }
- // if they dont have the minimum length how do you calculate extra characters needed
- //return !minLength ? std::max(minKneaded,missingCriteria)
- return !minLength ? 6-password.size()\
- : missingCriteria;
- }
|