始终输入密码 returns "not eligible"
Password input always returns "not eligible"
我现在正在学习 C。
我一直在开发一个程序来检查用户的输入(密码是否合格)。为了使密码被视为合格且相当强大,它至少需要具有以下项目列表中的一项:
- 大写字母;
- '$'符号;
- 字母数字字符;
在我的程序中,我创建了三个整数变量,用于记录上面提到的要求。
不幸的是,每当我输入 "correct" 版本的密码时,程序一直打印密码不合格。
请给我一个线索,我可能哪里错了。
//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;
}
isalpha
函数 returns 如果字符是大写或小写则为真。您在调用 isupper
的条件之前执行此操作。由于大写字符将满足第一个条件,因此第二个条件永远不会计算为真。
由于大写是字母数字的子集,因此您需要修改您的要求。相反,如果您想检查(例如):
- 大写
- 数字
- "$"
然后你将有一个条件使用 isupper
,一个使用 isdigit
,一个与 '$'
比较。
此外,您循环遍历 passwordInput
数组的所有元素,即使它们并未全部填充。不要测试 i<49
,而是使用 i<strlen(passwordInput)
.
我现在正在学习 C。
我一直在开发一个程序来检查用户的输入(密码是否合格)。为了使密码被视为合格且相当强大,它至少需要具有以下项目列表中的一项:
- 大写字母;
- '$'符号;
- 字母数字字符;
在我的程序中,我创建了三个整数变量,用于记录上面提到的要求。
不幸的是,每当我输入 "correct" 版本的密码时,程序一直打印密码不合格。
请给我一个线索,我可能哪里错了。
//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;
}
isalpha
函数 returns 如果字符是大写或小写则为真。您在调用 isupper
的条件之前执行此操作。由于大写字符将满足第一个条件,因此第二个条件永远不会计算为真。
由于大写是字母数字的子集,因此您需要修改您的要求。相反,如果您想检查(例如):
- 大写
- 数字
- "$"
然后你将有一个条件使用 isupper
,一个使用 isdigit
,一个与 '$'
比较。
此外,您循环遍历 passwordInput
数组的所有元素,即使它们并未全部填充。不要测试 i<49
,而是使用 i<strlen(passwordInput)
.