我必须检查密码。密码包含大写,小写,数字和至少8个字符时,密码很强。我写了这个C程序,但是它总是显示“弱密码”。我不知道原因。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 50
int strenght(char word[],int length)
{
int sup = 0;
int low = 0;
int dig = 0;
for(int i = 0; i < length; i++)
{
if(isupper(word[i]) == 1)
sup++;
else if(islower(word[i]) == 1)
low++;
else if(isdigit(word[i]) == 1)
dig++;
}
if(sup > 0 && low > 0 && dig > 0 && length >= 8)
return 1;
else
return 0;
}
int main()
{
printf("Type the password until '*'\n");
char word[N];
while(1)
{
printf("Password: ");
fgets(word, N, stdin);
int length = strlen(word) - 1;
if(word[0] == '*')
break;
else
{
if(strenght(word, length) == 1)
printf("Strong password\n");
if(strenght(word, length) == 0)
printf("Weak password\n");
}
}
return 0;
}
答案 0 :(得分:3)
问题是您正在比较isupper
的{{1}},islower
和isdigit
调用的结果。不要这样!如果不满足条件 ,则每个函数将返回零,如果满足条件,则返回 任何非零值 。 em>很满意。 (请参阅cppreference)。
因此,代替:
1
只需这样做:
if(isupper(word[i]) == 1)
sup++;
或者,如果您想保留比较的明确性质,请使用:
if(isupper(word[i]))
sup++;
(其他测试也类似。)