c - C中密码输入的验证

标签 c

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

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/

相关文章:

java - 使用 JNI 从 C 调用 Java

c - 如何使用 Pic32 打开连接到 PT6965 LED Controller 的 RGB LED?

c - 如何将 pcre2 修复为\w 将匹配标记?

c++ - 将 mpfr_t(或任何其他任意精度库类型)转换为 __float128

c - C网络编程,如何正确写入jpeg数据?

c - 为性能设计服务器的最佳方法是什么?

c - AVR PROGMEM 读取垃圾而不是字符串

c++ - 声明为 float& 类型的引用数组

c - 我在函数内的第一个返回值没有返回任何东西

c - 通过循环打印双数组