【问题标题】:How to check the strength of a password in C++ using bool functions如何使用布尔函数检查 C++ 中的密码强度
【发布时间】:2020-05-01 20:05:29
【问题描述】:

为我的班级做一个练习。我的老师希望我们使用 C-string,而不是 string 类。我们必须使用 bool 函数和 C-string 命令来确定密码是否足够强。我想我快要完成了,但我一定有什么错误?这是我得到的:

#include <iostream>
#include <cstring>
#include <cctype>

bool checklength(char[]);
bool checkdigit(char[]);
bool checklower(char[]);
bool checkupper(char[]);
bool checkspecial(char[]);

int main()
{
char pwdstr[20];

std::cout << "Enter your password\n";
std::cin >> pwdstr;

if (checklength(pwdstr) &&
    checkdigit(pwdstr) &&
    checklower(pwdstr) &&
    checkupper(pwdstr) &&
    checkspecial(pwdstr))
{
    std::cout << "Your password is strong.\n";
}

else
{
    std::cout << "Your password is too weak!\n";
}
}

bool checklength(char p[])
{
int i;
int len = strlen(p);

for (i = 0; i < len - 1;)
{
    if (isalnum(p[i]))
    {
        i++;
    }
}

if (i < 6)
{
    std::cout << "Your password must be at least 6 characters 
long.\n";
    return false;
}
else
{
    return true;
}
}

bool checkdigit(char p[])
{
int i;
int len = strlen(p);

for (i = 0; i < len - 1;)
{
    if (isdigit(p[i]))
    {
        i++;
    }
}

if (i < 1)
{
    std::cout << "Your password must have at least 1 digit in 
it.\n";
    return false;
}
else
{
    return true;
}
}

bool checklower(char p[])
{
int i;
int len = strlen(p);

for (i = 0; i < len - 1;)
{
    if (islower(p[i]))
    {
        i++;
    }
}

if (i < 1)
{
    std::cout << "Your password must have at least 1 lower case 
letter in it.\n";
    return false;
}
else
{
    return true;
}
}

bool checkupper(char p[])
{
int i;
int len = strlen(p);

for (i = 0; i < len - 1;)
{
    if (isupper(p[i]))
    {
        i++;
    }
}

if (i < 1)
{
    std::cout << "Your password must have at least 1 upper case 
letter in it.\n";
    return false;
}
else
{
    return true;
}
}

bool checkspecial(char p[])
{
int i;
int len = strlen(p);

for (i = 0; i < len - 1;)
{
    if (ispunct(p[i]))
    {
        i++;
    }
}

if (i < 1)
{
    std::cout << "Your password must have at least 1 special 
character in it.\n";
    return false;
}
else
{
    return true;
}
}

我在返回 false 之前添加错误描述之前的当前输出是,由于某种原因,一切都是正确的。现在我尝试的一切都表明我在 checklength 功能上失败了,密码太短了。

谢谢大家

【问题讨论】:

    标签: c++ passwords boolean c-strings


    【解决方案1】:

    你所有的循环都是错误的,你混淆了你正在测试的字符的索引,以及通过测试的字符数的计数,你需要单独的变量。另外,您对字符串长度的长度有误,您有len - 1 而不是len。 所以这个

    bool checklength(char p[])
    {
        int i;
        int len = strlen(p);
    
        for (i = 0; i < len - 1;)
        {
            if (isalnum(p[i]))
            {
                i++;
            }
        }
    
        if (i < 6)
        {
            std::cout << "Your password must be at least 6 characters long.\n";
            return false;
        }
        else
        {
            return true;
        }
    }
    

    应该是这样的

    bool checklength(char p[])
    {
        int count = 0;
        int len = strlen(p);        
        for (int i = 0; i < len; i++)
        {
            if (isalnum(p[i]))
                count++;
        }
        if (count < 6)
        {
            std::cout << "Your password must be at least 6 characters long.\n";
            return false;
        }
        else
        {
            return true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 STL 算法可以大大简化您的函数。例如checklength 可能是:

      bool checklength(char p[])
      {
        return std::count_if(p, p + strlen(p), [](unsigned char c) {
                              return std::isalnum(c); 
                             }) >= 6;
      }
      

      你可以为其他功能做类似的事情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-18
        • 2011-09-03
        • 1970-01-01
        • 2010-11-26
        • 2014-10-26
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多