如何使用布尔函数检查 C++ 中密码的强度

How to check the strength of a password in C++ using bool functions

正在为我的 class 做一个练习。我的老师希望我们使用 C 字符串,而不是字符串 class。我们必须使用 bool 函数和 C 字符串命令来确定密码是否足够强。我想我快完成了,但我一定有什么地方出错了?这是我得到的:

#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 之前添加错误描述之前,我当前的输出是由于某种原因一切都是正确的。现在我尝试的一切都说我在校验长度函数上失败了,密码太短了。

谢谢大家

你所有的循环都是错误的,你混淆了你正在测试的字符的索引,以及通过测试的字符数,你需要单独的变量。另外,您对字符串长度的长度有错误,您有 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;
    }
}

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

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

您可以对其他功能做类似的事情。