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

问题描述 投票:0回答:2

[为我的班级做练习。我的老师希望我们使用C弦,而不是弦类。我们必须使用布尔函数和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之前的当前输出是,由于某种原因,一切都正确。现在,我尝试的所有内容都表明我在checklength函数上失败,密码太短。

谢谢所有

c++ passwords boolean c-strings
2个回答
2
投票

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

0
投票

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

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

并且您可以为其他功能做类似的事情。

© www.soinside.com 2019 - 2024. All rights reserved.