code.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Create a function that determines the minimum number of characters needed to make a strong password.
  2. * A password is considered strong if it satisfies the following criteria:
  3. * Its length is at least 6.
  4. * It contains at least one digit.
  5. * It contains at least one lowercase English character.
  6. * It contains at least one uppercase English character.
  7. * It contains at least one special character: !@#$%^&*()-+
  8. * Types of characters in a form you can paste into your solution:
  9. */
  10. bool isSpecial(char c){
  11. return c == '!' || c == '@' || c == '#' || c == '$' ||\
  12. c == '%' || c == '^' || c == '&' || c == '*' ||\
  13. c == '(' || c == ')' || c == '-' || c == '+';
  14. }
  15. int strongPassword(std::string password) {
  16. bool hasDigit = false;
  17. bool hasLower = false;
  18. bool minLength = password.size()>=6;
  19. bool hasUpper = false;
  20. bool hasSpecial = false;
  21. for(int i=0;i<password.size();i++){
  22. if(isdigit(password[i])){hasDigit=true;}
  23. if(islower(password[i])){hasLower=true;}
  24. if(isupper(password[i])){hasUpper=true;}
  25. if(isSpecial(password[i])){hasSpecial=true;}
  26. }
  27. // return hasDigit && hasLower && minLength && hasUpper && hasSpecial;
  28. // return !hasDigit + !hasLower + !minLength + !hasUpper + !hasSpecial;
  29. // number of criteria have matched
  30. int criteriaMatch = (hasDigit+hasLower+hasUpper+hasSpecial);
  31. // number of missing criteria
  32. int missingCriteria =(!hasDigit+!hasLower+!hasUpper+!hasSpecial);
  33. // the number of non-contributing characters
  34. int fluff = password.size() - criteriaMatch;
  35. // if the number of missing criteria would not add up to six characters
  36. // we need to add, additional characters.
  37. int minKneaded = 6 - fluff;
  38. // int total = fluff + missingCriteria;
  39. // if(total<6){
  40. // return missingCriteria + 6-total;
  41. // }
  42. // if they dont have the minimum length how do you calculate extra characters needed
  43. //return !minLength ? std::max(minKneaded,missingCriteria)
  44. return !minLength ? 6-password.size()\
  45. : missingCriteria;
  46. }