【问题标题】:Password input always returns "not eligible"密码输入总是返回“不合格”
【发布时间】: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


【解决方案1】:

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

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

  • 大写
  • 数字
  • “$”

那么你将有一个条件使用isupper,一个使用isdigit,一个使用'$'

此外,您循环遍历 passwordInput 数组的所有元素,即使它们没有全部填充。不要测试i&lt;49,而是使用i&lt;strlen(passwordInput)

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 2015-01-05
    • 2012-09-19
    • 2018-07-06
    • 2021-06-24
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多