密码合规检测

题目描述

网站注册需要有用户名和密码, 编写程序以检查用户输入密码的有效性。 合
规的密码应满足以下要求:
1、只能由 a-z 之间 26 个小写字母、A-Z 之间 26 个大写字母、0-9 之间 10个数字以及!@#$四个特殊字符构成。
2、密码最短长度:6个字符,密码最大长度:12个字符。
3、大写字母、小写字母和数字必须至少有其中两种,以及至少有四个特殊字符中的一个。

输入描述

输入一行不含空格的字符串。约定长度不超过 100。该字符串被英文逗号分隔为多段,作为多组被检测密码。

输出描述

输出若干行,每行输出一组合规的密码。
输出顺序以输入先后为序,即先输入则先输出。

样例输入 1

seHJ12!@,sjdkffH$123,sdf!@&12HDHa!,123&^YUhg@!

样例输出 1

seHJ12!@
sjdkffH$123

代码

#include <bits/stdc++.h>
using namespace std;
char line[101];
char pwd[101];
bool check(char * str,int l){//判断密码是否合规
    if(l<6 || l>12)
        return false;
    bool d=false,x=false,s=false,f=false;
    for(int i=0;str[i]!='\0';i++){
        if('A'<=str[i] && str[i]<='Z')
            d=true;
        else if('a'<=str[i] && str[i]<='z')
            x=true;
        else if('0'<=str[i] && str[i]<='9')
            s=true;
        else if(str[i]=='!' || str[i]=='@' || str[i]=='#' || str[i]=='$')
            f=true;
        else
            return false;
    }
    if(!f)
        return false;
    if(d+x+s<2)
        return false;
    return true;
}
int main(){
    cin>>line;
    int len=0;
    for(int i=0;line[i]!='\0';i++){
        if(line[i]!=','){
            pwd[len]=line[i];
            len++;
        }
        else{
            pwd[len]='\0';
            if(check(pwd,len))
                cout<<pwd<<endl;
            len=0;
        }
    }
    if(len>0){//判断最后一个密码
        pwd[len]='\0';
        if(check(pwd,len))
            cout<<pwd<<endl;
    }
 return 0;
}

解释

输入被英文逗号分为了四组被检测密码:“seHJ12!@”、“sjdkffH$123”、“ sdf!@&12HDHa!” 、“123&^YUhg@!”。其中,“sdf!@&12HDHa!”长度超过 12“sdf!@&12HDHa!”个字符,不合规;“123&YUhg@!”包含四个特殊字符之外的字符“”,不合规。

### 密码合规检测C++程序设计 密码合规检测是一种常见的功能需求,用于验证用户输入的密码是否满足特定的安全标准。以下是基于 C++ 的一种实现方式及其分析。 #### 实现思路 为了确保密码符合一定的安全性要求,可以定义一系列规则来约束密码的内容。这些规则通常包括但不限于以下几点: 1. 长度至少为某个最小值(如8字符)。 2. 至少包含一个小写字母、大写字母、数字以及特殊字符中的若干种组合。 3. 不应包含连续重复的字符或其他易被猜测的部分。 下面是一个简单的 C++ 程序示例,展示如何通过函数判断给定字符串是否符合上述条件: ```cpp #include <iostream> #include <string> using namespace std; bool hasLower(const string& password) { for (char c : password) { if (islower(c)) return true; } return false; } bool hasUpper(const string& password) { for (char c : password) { if (isupper(c)) return true; } return false; } bool hasDigit(const string& password) { for (char c : password) { if (isdigit(c)) return true; } return false; } bool hasSpecialChar(const string& password) { const string specialChars = "!@#$%^&*()-_=+[]{}|;:',.<>/?"; for (char c : password) { if (specialChars.find(c) != string::npos) return true; } return false; } bool isValidPassword(const string& password, int minLength = 8) { if (password.length() < minLength) return false; bool lower = hasLower(password); bool upper = hasUpper(password); bool digit = hasDigit(password); bool special = hasSpecialChar(password); // At least three of the four conditions must be met. return (int(lower) + int(upper) + int(digit) + int(special)) >= 3; } int main() { cout << "Enter your password: "; string password; cin >> password; if (isValidPassword(password)) { cout << "Password is valid." << endl; } else { cout << "Password does not meet security requirements." << endl; } return 0; } ``` #### 功能解释 此代码实现了几个辅助函数分别用来检查是否存在小写字母[^4]、大写字母[^5]、数字[^6]和特殊字符[^7]。`isValidPassword()` 函数综合考虑了以上因素并允许自定义最短长度,默认设为8位。如果密码长度不足或未能达到规定的复杂程度,则返回 `false` 表明该密码不符合设定的标准。 #### 编译注意事项 如果你正在使用的开发环境是 Dev-C++ 并希望启用某些现代特性支持的话,请记得调整编译参数以兼容 C++11 或更高版本标准[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值