我正在尝试验证 C 中的密码,并且else
每当代码运行时,我的其中一条语句就会自动触发。我运行测试以查看字符是否为符号,以及是否将 1 添加到int symbol
using symbol++;
,但问题是无论我正在测试的字符是否为符号,此代码都在执行。
我认为这个问题与我的if, else
陈述的结构有关,我尝试了几种组合,但有一点是错误的,它使我使用过的程序无法使用else if
,但这并没有帮助。这似乎应该很明显,但我似乎无法弄清楚出了什么问题。
char password[30];
int lower, upper, number, symbol, i;
lower = upper = number = symbol = 0;
printf("Enter your password: ");
scanf("%s", &password);
int len = strlen(password);
for (i = 0; i <= len; i++) {
if (isalpha(password[i])){
if (isupper(password[i])){
upper++;
}
else{
lower++;
}
}
if (isdigit(password[i])){
number++;
}
else{
symbol++;
}
}
if (upper >= 1 && lower >= 1 && number >= 1 && symbol >= 1 && len >=6){
printf("Your password is good!");
}
if (upper < 1){
printf("You need an uppercase letter \n");
}
if (lower < 1){
printf("You need a lowercase letter \n");
}
if (number < 1){
printf("You need a number \n");
}
if (symbol < 1){
printf("You need a symbol \n");
}
if (len < 6){
printf("Your password must be at least 6 characters \n");
}