题目:检查密码是否符合规定
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
思路:对于要求3,以3个字符为一个子串,每次向后移一个,依次对比检查
心得体会:
通常用到的字符,其ASCII码分布:
‘\0’ : 0 '0'~'9': 48~57 'A'~'Z' : 65~90 'a' ~ 'z' : 97~122
其他ASCII码值则对应特殊字符
/*************************************************************************
> File Name: e22.c
> Author: LNM
> Mail: liunenming@gmail.com
> Created Time: 2018年09月13日 星期四 10时28分05秒
>function:
************************************************************************/
#include<stdio.h>
#include<string.h>
#define MAX 1000
int Check_Type(char *);
int Check_Repeat(char *);
int main()
{
char str[MAX];
int i,j,len;
while(fgets(str,MAX,stdin))
{
len = strlen(str);
str[--len] = '\0';
if(len <= 8)
{
printf("NG\n");
continue;
}
if(!(Check_Type(str)))
{
printf("NG\n");
continue;
}
if(!(Check_Repeat(str)))
{
printf("NG\n");
continue;
}
printf("OK\n");
}
return 0;
}
int Check_Type(char *str)
{
int typenum,type[4] = {0};
int i,len;
typenum = 0;
len = strlen(str);
for(i = 0;i < len;i ++)
{
if((str[i] < '0') && (str[i] > 0))
type[0] ++;
else if((str[i] >= '0') && (str[i] <= '9'))
type[1] ++;
else if((str[i] >'9') && (str[i] < 'A'))
type[0] ++;
else if((str[i] >= 'A') && (str[i] <= 'Z'))
type[2] ++;
else if((str[i] >'Z') && (str[i] < 'a'))
type[0] ++;
else if((str[i] >='a') && (str[i] <= 'z'))
type[3] ++;
else if((str[i] >'z'))
type[0] ++;
}
for(i = 0;i < 4;i ++)
{
if(type[i] > 0)
typenum ++;
}
if(typenum < 3)
return 0;
else
return 1;
}
int Check_Repeat(char *str) //检查3位的子串
{
char tmp1[4],tmp2[4];
int p1,p2,len;
p1 = 0;
memset(tmp1,0,4);
memset(tmp2,0,4);
len = strlen(str);
for(p1 = 0;p1 < len;p1 ++)
{
if((str[p1+1] == '\0') || (str[p1+2] == '\0'))
break;
strncpy(tmp1,&str[p1],3);
for(p2 = p1 + 1;p2 < len;p2 ++)
{
strncpy(tmp2,&str[p2],3);
if(strcmp(tmp1,tmp2) == 0)
return 0;
}
}
return 1;
}