Ad

Instead of functions, i used lambdas that captured the password by reference.
So no additional passing through functions is necessary.
Invoking the lambdas as late as possible. To call only 1 function in the best case.
Using std::string::find_first_of() to use the stl instead of writing the search by myself.

Code
Diff
  • #include <string>
    bool testPassword(std::string password)
    {
        auto const has_length{[&]() { return password.size() >= 8; }};
        auto const has_upper{[&]() { return password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos; }};
        auto const has_special{[&]() { return password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos; }};
        auto const has_digit{[&]() { return password.find_first_of("0123456789") != std::string::npos; }};
        return has_length() && has_upper() && has_special() && has_digit();
    }
    • 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)
    • #include <string>
    • bool testPassword(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;
    • auto const has_length{[&]() { return password.size() >= 8; }};
    • auto const has_upper{[&]() { return password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos; }};
    • auto const has_special{[&]() { return password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos; }};
    • auto const has_digit{[&]() { return password.find_first_of("0123456789") != std::string::npos; }};
    • return has_length() && has_upper() && has_special() && has_digit();
    • }