安全密码(字符串)

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;
}
相关推荐
学学酱快乐22 分钟前
2026年PMP考试生命周期选择决策树精讲:从需求判断到混合型统一框架的应试全攻略
算法·决策树·机器学习·pmp新版考试大纲·pmp新题型·pmp培训哪家好·pmp培训机构推荐
傅科摆 _ py35 分钟前
动态规划复习
算法·动态规划
Wang's Blog1 小时前
PostgreSQL笔记49:向量检索核心算法、索引调优与过滤策略深度解析
笔记·算法·postgresql
疯狂打码的少年1 小时前
【数据结构】交换类排序:冒泡与快速排序
数据结构·笔记·算法·排序算法
Nil2082 小时前
leetcode 108有序数组转换为二叉搜索树
数据结构·算法·leetcode
高频因子挖掘机2 小时前
QuantDash 成交量单位统一实战:从“手”到“股”的跨市场量化数据清洗全流程
后端·算法·github
hn小菜鸡2 小时前
LeetCode 763、划分字母区间
数据结构·算法·leetcode
Escalating_xu2 小时前
【C++ STL简介】从六大组件到容器、迭代器与算法协作
java·c++·算法
纪念 2293 小时前
算法二叉树(一)
算法
疯狂打码的少年3 小时前
【数据结构】哈希表:构造与冲突处理
数据结构·笔记·哈希算法·散列表