题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

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

//11.12-11.34 20分钟
int main() {
    char str[102] = {0};

    while (fgets(str, sizeof(str), stdin) != NULL) {
        str[strcspn(str, "\r")] = 0;
        str[strcspn(str, "\n")] = 0;

        int len = strlen(str);
        if (len <= 8) {
            printf("NG\n");
            continue;
        }
        
        //检查大小写等
        char lower = 0, upper = 0, dig = 0, sign = 0;
        int i = 0;
        for (i = 0; i < len; i++) {
            //printf("str[i]:%c\n", str[i]);
            if (str[i] >= 'a' && str[i] <= 'z') {
                lower = 1;
            } else if (str[i] >= 'A' && str[i] <= 'Z') {
                upper = 1;
            } else if (str[i] >= '0' && str[i] <= '9') {
                dig = 1;
            } else {
                sign = 1;
            }

            if (lower + upper + dig + sign >= 3) {
                break;
            }
        }
        if ((lower + upper + dig + sign) < 3) {
            printf("NG\n");
            //printf("%d %d %d %d\n", lower , upper , dig , sign);
            continue;
        }

        //检查字串
        char sub_str[4] = {0};
        char repeat_substr = 0;
        for (i = 0; i < len-3; i++) {
            memset(sub_str, 0, sizeof(sub_str));
            memcpy(sub_str, str+i, 3);
            if (strstr(str+i+1, sub_str)) {
                repeat_substr = 1;
                break;
            }
        }

        if (repeat_substr) {
            printf("NG\n");
        }
        else {
            printf("OK\n");
        }


        memset(str, 0, sizeof(str));
    }
    return 0;
}

全部评论

相关推荐

蚂蚁 基架java (n+6)*16 签字费若干
点赞 评论 收藏
分享
点赞 评论 收藏
分享
10-16 09:58
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务