输入的密码始终返回“不符合条件”

BB8

我正在学习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;
}
dbush

isalpha如果字符是大写或小写,则函数返回true。您在调用条件之前执行此操作isupper由于大写字符将满足第一个条件,因此第二个条件将永远不会评估为true。

由于大写字母是字母数字的一部分,因此您需要修改要求。相反,如果您要检查(例如):

  • 大写
  • 数字
  • “ $”

然后,您将具有一种条件使用isupper,一种用途isdigit和一种与之比较'$'

同样,passwordInput即使没有全部填充,也要遍历数组的所有元素代替测试i<49,使用i<strlen(passwordInput)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章