Ad

Made into a class and adjusted the test case accordingly. Standardized naming of functions and variables for consistency.

Code
Diff
  • class Password{
        public:
            static bool hasCap(const std::string& password);
            static bool hasSpec(const std::string& password);
            static bool hasDigit(const std::string& password);
            static bool hasLength(const std::string& password);
            static bool testPassword(const std::string& password);
    };
            
    bool Password::hasCap(const std::string& password)
    {
        bool result = false;
        for(auto symbol : password)
        {
            if(symbol >= 'A' && symbol <= 'Z')
            {
                result = true;
                break;
            }
        }
        return result;
    }
    
    bool Password::hasSpec(const std::string& password)
    {  
        bool result = false;
        
        const std::string specials("!\"#$%&'()*+'-./;:<>=?");
        for(auto spec : specials)
        {
            if(password.find(spec) != -1)
            {
                result = true;
                break;
            }
        }    
        return result;
    }
    
    bool Password::hasDigit(const std::string& password)
    {
        bool result = false;
        for(auto symbol : password)
        {
            if(symbol >= '0' && symbol <= '9')
            {
                result = true;
                break;
            }
        }
        return result;
    }
    
    bool Password::hasLength(const std::string& password)
    {
        return password.length() > 7;
    }
    
    bool Password::testPassword(const std::string& password)
    {
      bool cap = Password::hasCap(password);
      bool spec = Password::hasSpec(password);
      bool digit = Password::hasDigit(password);
      bool number = Password::hasLength(password);
      
      return cap && spec && digit && number;
    }
    • #include <string>
    • bool hasCapital(const std::string& password)
    • class Password{
    • public:
    • static bool hasCap(const std::string& password);
    • static bool hasSpec(const std::string& password);
    • static bool hasDigit(const std::string& password);
    • static bool hasLength(const std::string& password);
    • static bool testPassword(const std::string& password);
    • };
    • bool Password::hasCap(const std::string& password)
    • {
    • bool result = false;
    • for(auto symbol : password)
    • {
    • if(symbol >= 'A' && symbol <= 'Z')
    • {
    • result = true;
    • break;
    • }
    • }
    • return result;
    • }
    • bool hasSpecial(const std::string& password)
    • bool Password::hasSpec(const std::string& password)
    • {
    • bool result = false;
    • const std::string specials("!\"#$%&'()*+'-./;:<>=?");
    • for(auto spec : specials)
    • {
    • if(password.find(spec) != -1)
    • {
    • result = true;
    • break;
    • }
    • }
    • return result;
    • }
    • bool hasDigit(const std::string& password)
    • bool Password::hasDigit(const std::string& password)
    • {
    • bool result = false;
    • for(auto symbol : password)
    • {
    • if(symbol >= '0' && symbol <= '9')
    • {
    • result = true;
    • break;
    • }
    • }
    • return result;
    • }
    • bool isLongPassword(const std::string& password)
    • bool Password::hasLength(const std::string& password)
    • {
    • return password.length() > 7;
    • }
    • bool testPassword(const std::string& password)
    • bool Password::testPassword(const std::string& password)
    • {
    • bool cap = hasCapital(password);
    • bool spec = hasSpecial(password);
    • bool digit = hasDigit(password);
    • bool number = isLongPassword(password);
    • bool cap = Password::hasCap(password);
    • bool spec = Password::hasSpec(password);
    • bool digit = Password::hasDigit(password);
    • bool number = Password::hasLength(password);
    • return cap && spec && digit && number;
    • }

In this Kata you will need to write a function that returns whether a password is strong or not.

Your code should return a Boolean of true if the provided password meets the following conditions

  1. password must be no less than 8 characters long
  2. password must have at least 1 capital letter
  3. password must have at least 1 number
  4. password must have at least 1 special character

for this Kata special characters are considered ( ! " # $ % & ' ( ) * + ' - . / ; : < > = or ?)

if the password doesn't meet these requirements return false.

Code
Diff
  • #include <string>
    
    bool testPassword(std::string password)
    {
      bool cap = false;
      bool spec = false;
      bool number = false;
      char temp;
      
      for(int i=0;i<password.length();i++)
      {
        temp = password[i];
        if ((temp > 32 && temp < 48) || (temp > 57 && temp < 64))
        {
          spec = true;
        }
      }
    //check each digit and see if any are capital letters
    for(int i=0;i<password.length();i++)
    {
      if (password[i] > 64 && password[i] < 91)
      {
        cap = true;
      }
    }
    
    //see if the password is over 8 digits long
    if (password.length() > 7)
    {
      number = true;
    }
      
      //provide final answer
      if (cap && number && spec)
      {
        return true;
      } 
      else 
      {
        return false;
      }
    
    }
    • function testPassword(password){
    • var cap = false;
    • var spec = false;
    • var number = false;
    • var temp;
    • #include <string>
    • bool testPassword(std::string password)
    • {
    • bool cap = false;
    • bool spec = false;
    • bool number = false;
    • char temp;
    • for(i=0;i<password.length;i++){
    • temp = password[i].charCodeAt();
    • if(temp > 32 && temp < 48){
    • spec = true;
    • } else if(temp > 57 && temp < 64){
    • for(int i=0;i<password.length();i++)
    • {
    • temp = password[i];
    • if ((temp > 32 && temp < 48) || (temp > 57 && temp < 64))
    • {
    • spec = true;
    • }
    • }
    • //check each digit and see if any are capital letters
    • for(i=0;i<password.length;i++){
    • if(password.charCodeAt(i) > 64 && password.charCodeAt(i) < 91){
    • for(int i=0;i<password.length();i++)
    • {
    • if (password[i] > 64 && password[i] < 91)
    • {
    • cap = true;
    • }
    • }
    • //see if the password is over 8 digits long
    • if(password.length > 7){
    • if (password.length() > 7)
    • {
    • number = true;
    • }
    • //provide final answer
    • if(cap && number && spec){
    • if (cap && number && spec)
    • {
    • return true;
    • } else {
    • }
    • else
    • {
    • return false;
    • }
    • }