CVTE的一道笔试题:
判断密码设置是否满足要求,要求如下:
- 长度大于等于8位且小于等于16位
- 至少包含大写字母、小写字母、特殊字符和数字中的三种
- 密码中不能包含连续相同的字符串,如cc,cvtecvte等都是不符合规范的
C语言实现如下:
/*************************************************************************
> File Name: PasswdStandard.c
> Author: zhuxinquan
> Mail: [email protected]
> Created Time: 2017年02月27日 星期一 09时44分50秒
************************************************************************/
/*
* 密码需要满足如下要求:
* 1.长度大于等于8且小于等于16位
* 2.至少包含大写字母、小写字母、特殊字符和数字中的三种
* 3.密码中不能包含连续相同的字符串,如cc,cvtecvte等都是不符合规范的
*/
#include<stdio.h>
#include<stdlib.h>
/*
* 判断输入密码的长度和密码中字符的种类是否符合规范
*/
int countTypeAndLengthOk(char * s){
int A_Z = ;
int a_z = ;
int number = ;
int specialCharacter = ;
int i = ;
int length = ;
//统计密码长度和使用字符的种类
while(s[i] != '\0'){
length++;
if(s[i] >= 'A' && s[i] <= 'Z'){
A_Z = ;
}else if(s[i] >= 'a' && s[i] <= 'z'){
a_z = ;
}else if(s[i] >= '0' && s[i] <= '9'){
number = ;
}else{
specialCharacter = ;
}
i++;
}
if(length >= && length <= && (A_Z + a_z + number + specialCharacter) >= ){
return ;
}
return ;
}
/**
* 判断是否包含连续相同的子串
*/
int noContainContinueString(char * s){
int left, right, tempLeft, tempRight;
for(left = ; s[left] != '\0'; left++){
for(right = left + ; s[right] != '\0'; right++){
if(s[left] == s[right] && (right - left) == ){
return ;
}else if(s[left] == s[right]){
tempLeft = left;
tempRight = right;
while(s[tempRight] != '\0' && s[tempLeft] == s[tempRight]){
if(right - tempLeft == ){
return ;
}
tempLeft++;
tempRight++;
}
}
}
}
return ;
}
int isStandardPasswd(char * s){
if(!countTypeAndLengthOk(s) || !noContainContinueString(s)){
return ;
}
return ;
}
int main(int argc, char * argv[]){
if(argc < ){
printf("Usage: %s \'PasswdString\' \n", argv[]);
exit(EXIT_FAILURE);
}
int boolean = isStandardPasswd(argv[]);
if(boolean){
printf("Is Standard Passwd!\n");
}else{
printf("Not Standard Passwd!\n");
}
}