Ad

My version with few improvements.

Code
Diff
  • #include <string>
    
    bool hasCapital(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 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 result = false;
        for(auto symbol : password)
        {
            if(symbol >= '0' && symbol <= '9')
            {
                result = true;
                break;
            }
        }
        return result;
    }
    
    bool isLongPassword(const std::string& password)
    {
        return password.length() > 7;
    }
    
    bool testPassword(const std::string& password)
    {
      bool cap = hasCapital(password);
      bool spec = hasSpecial(password);
      bool digit = hasDigit(password);
      bool number = isLongPassword(password);
      
      return cap && spec && digit && number;
    }
    • #include <string>
    • bool hasCapital(std::string password)
    • bool hasCapital(const std::string& password)
    • {
    • for(int i=0; i < password.length(); i++)
    • {
    • if (password[i] > 64 && password[i] < 91)
    • bool result = false;
    • for(auto symbol : password)
    • {
    • return true;
    • if(symbol >= 'A' && symbol <= 'Z')
    • {
    • result = true;
    • break;
    • }
    • }
    • }
    • return false;
    • return result;
    • }
    • bool hasSpecial(std::string password)
    • bool hasSpecial(const std::string& password)
    • {
    • char temp;
    • for(int i=0;i<password.length();i++)
    • {
    • temp = password[i];
    • if ((temp > 32 && temp < 48) || (temp > 57 && temp < 64))
    • bool result = false;
    • const std::string specials("!\"#$%&'()*+'-./;:<>=?");
    • for(auto spec : specials)
    • {
    • return true;
    • if(password.find(spec) != -1)
    • {
    • result = true;
    • break;
    • }
    • }
    • return result;
    • }
    • bool hasDigit(const std::string& password)
    • {
    • bool result = false;
    • for(auto symbol : password)
    • {
    • if(symbol >= '0' && symbol <= '9')
    • {
    • result = true;
    • break;
    • }
    • }
    • }
    • return false;
    • return result;
    • }
    • bool isLongPassword(std::string password)
    • bool isLongPassword(const std::string& password)
    • {
    • return password.length() > 7;
    • return password.length() > 7;
    • }
    • bool testPassword(std::string password)
    • bool testPassword(const std::string& password)
    • {
    • bool cap = hasCapital(password);
    • bool spec = hasSpecial(password);
    • bool digit = hasDigit(password);
    • bool number = isLongPassword(password);
    • //provide final answer
    • if (cap && number && spec)
    • {
    • return true;
    • }
    • else
    • {
    • return false;
    • }
    • return cap && spec && digit && number;
    • }