gpt4 book ai didi

c - C中密码输入的验证

转载 作者:行者123 更新时间:2023-11-30 17:01:23 24 4
gpt4 key购买 nike

我不知道如何进一步处理我即将创建的这个程序。这是这个想法:

Validate the password input to check if the password has at least one uppercase letter, lowercase letter and a number.

目前它的某些部分已损坏。例如虚假的、真实的陈述。以及主函数中的“非动态”字符数组。我现在也不知道该怎么做。但它解释了我在寻找什么。

那么如何在不编写太多代码的情况下验证这一点?

这是我当前的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int passval(char pw[])
{
int x;

for (x = 0; x < sizeof(pw); x++) {
if ( (isalnum(pw[x])) || (ispunct(pw[x])) ) {
return 0;
} else {
return 1;
}
}

return 0;
}

int main()
{
char password[20];

printf("Enter password: ");
scanf("%s", password);

if (passval(password) == TRUE) {
printf("Password is TRUE");
}

return 0;
}

最佳答案

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int
password_validate(const char *pass)
{
int upper = 0, lower = 0, digit = 0;
if (pass == NULL || strlen(pass) == 0)
return -1;

do
{
if (isupper(*pass))
upper = 1;
if (islower(*pass))
lower = 1;
if (isdigit(*pass))
digit = 1;
} while ((!lower || !upper || !digit) && *(++pass));

return (*pass != '\0' ? 0 : (upper == 0 ? -2 : (lower == 0 ? -3 : -4)));
}

请查看下面的代码示例链接,以确保了解一些极端情况(感谢 Alex Pogue 突出显示其他情况)以及此函数如何处理它们。

https://ideone.com/GiOGkj

关于c - C中密码输入的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37101849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com