安全密码(字符串)

cpp 复制代码
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool is_secure_password(const char* password);
int main()
{
    int M;
    char password[51];
    // 读取输入中的密码数量 M
    scanf("%d", &M);
    // 处理每个密码
    for (int i = 0; i < M; ++i)
    {
        // 读取密码
        scanf("%s", password);

        // 检查密码是否安全并输出结果
        if (is_secure_password(password))
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}
// 函数定义
bool is_secure_password(const char* password)
{
    // 检查密码长度
    int len = strlen(password);
    if (8 <= len && len <= 16)
    {
        // 检查密码字符类别
        bool categories[4] = { false };  // 大写字母、小写字母、数字、特殊符号
        for (int i = 0; i < len; ++i) {
            char current = password[i];
            if ('A' <= current && current <= 'Z')
            {
                categories[0] = true;
            }
            else if ('a' <= current && current <= 'z')
            {
                categories[1] = true;
            }
            else if ('0' <= current && current <= '9')
            {
                categories[2] = true;
            }
            else if (strchr("~!@#$%^&", current) != NULL)
            {
                categories[3] = true;
            }
        }
        // 判断密码是否包含至少三组字符类别
        if (categories[0] + categories[1] + categories[2] + categories[3] >= 3)
        {
            return true;
        }
    }
    return false;
}
相关推荐
bkspiderx30 分钟前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐2 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸2 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
tju新生代魔迷3 小时前
数据结构:双向链表
数据结构·链表
一只懒洋洋3 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类
方案开发PCBA抄板芯片解密4 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_994 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
学c语言的枫子4 小时前
数据结构——双向链表
c语言·数据结构·链表
小白狮ww4 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥4 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud