安全密码(字符串)

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;
}
相关推荐
秋难降3 分钟前
LRU缓存算法(最近最少使用算法)——工业界缓存淘汰策略的 “默认选择”
数据结构·python·算法
CoovallyAIHub1 小时前
线性复杂度破局!Swin Transformer 移位窗口颠覆高分辨率视觉建模
深度学习·算法·计算机视觉
点云SLAM2 小时前
Eigen中Dense 模块简要介绍和实战应用示例(最小二乘拟合直线、协方差矩阵计算和稀疏求解等)
线性代数·算法·机器学习·矩阵·机器人/slam·密集矩阵与向量·eigen库
Jayyih2 小时前
嵌入式系统学习Day19(数据结构)
数据结构·学习
renhongxia12 小时前
大模型微调RAG、LORA、强化学习
人工智能·深度学习·算法·语言模型
DdduZe3 小时前
8.19作业
数据结构·算法
PyHaVolask3 小时前
链表基本运算详解:查找、插入、删除及特殊链表
数据结构·算法·链表
高山上有一只小老虎3 小时前
走方格的方案数
java·算法
吧唧霸3 小时前
golang读写锁和互斥锁的区别
开发语言·算法·golang