【发布时间】:2020-05-24 03:36:52
【问题描述】:
我现在正在学习 C。
我一直在开发一个检查用户输入(密码资格)的程序。为了使密码被视为合格且相当强大,它需要至少具有以下项目列表中的一项:
- 大写字母;
- '$' 符号;
- 字母数字字符;
在我的程序中,我创建了三个整数变量,用于记录上述要求。
不幸的是,每当我输入“正确”版本的密码时,程序都会不断打印密码不合格。
请给我一个线索,我可能错了。
//challenge:
//build a program that checks when user enters a password for an uppercase letter, a number, and a dollar sign.
//if it does output that password is good to go.
int main()
{
char passwordInput[50];
int alphaNumericCount = 0;
int upperCharacterCount = 0;
int dollarCount = 0;
printf("Enter you password:\n");
scanf(" %s", passwordInput);
//int charactersAmount = strlen(tunaString);
for (int i = 0; i < 49; i++){
//tunaString[i]
if( isalpha(passwordInput[i]) ) {
alphaNumericCount++;
//continue;
}else if( isupper(passwordInput[i]) ) {
upperCharacterCount++;
//continue;
}else if( passwordInput[i] == '$' ) {
dollarCount++;
//continue;
}
}
if( (dollarCount == 0) || (upperCharacterCount == 0) || (alphaNumericCount == 0) ){
printf("Your entered password is bad. Work on it!\n");
}else{
printf("Your entered password is good!\n");
}
return 0;
}
【问题讨论】:
-
提示:如果用户输入的密码不完全是 49 个字符会怎样?
-
您的
else if条件未运行,因为第一个if成功。他们需要独立ifs。
标签: c password-checker