描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1≤n≤100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK
实现
#include <stdio.h>
#include <string.h>
#define M_STR_OK "OK"
#define M_STR_NG "NG"
void fmtBuf(char *buf)
{
int len = strlen(buf);
for (int i = 0; i < len; i++) {
if ((buf[i] == '\r') || (buf[i] == '\n')) {
buf[i] = '\0';
}
}
}
int checkLen(char *buf)
{
return ((strlen(buf) > 8) ? 0 : -1);
}
int checkType(char *buf)
{
int len = strlen(buf);
int type, typeUper = 0, typeLower = 0, typeNum = 0, typeOther = 0;
for (int i = 0; i < len; i++) {
if ((buf[i] >= 'A') && (buf[i] <= 'Z')) {
typeUper = 1;
} else if ((buf[i] >= 'a') && (buf[i] <= 'z')) {
typeLower = 1;
} else if ((buf[i] >= '0') && (buf[i] <= '9')) {
typeNum = 1;
} else {
if ((buf[i] != ' ') && (buf[i] != '\n')) {
typeOther = 1;
}
}
}
type = typeUper + typeLower + typeNum + typeOther;
// printf("%d + %d + %d + %d = %d\n", typeUper, typeLower, typeNum, typeOther, type);
return ((type >= 3) ? 0 : -1);
}
int checkCommonStr(char *buf)
{
int i, j;
int len = strlen(buf);
for (i = 0; i < len - 5; i++) {
for (j = i + 3; j < len - 2; j++) {
if ((buf[i] == buf[j]) && (buf[i+1] == buf[j+1]) && (buf[i+2] == buf[j+2])) {
// printf("i: %d, j: %d,[%c%c%c]\n", i, j, buf[i], buf[i+1], buf[i+2]);
return -1;
}
}
}
return 0;
}
int main()
{
int len;
char inBuf[101] = {0};
while (fgets(inBuf, sizeof(inBuf), stdin)) {
fmtBuf(inBuf);
// printf("inBuf: %s\n", inBuf);
if (checkLen(inBuf) < 0) {
printf("%s\n", M_STR_NG);
continue;
}
if (checkType(inBuf) < 0) {
printf("%s\n", M_STR_NG);
continue;
}
if (checkCommonStr(inBuf) < 0) {
printf("%s\n", M_STR_NG);
continue;
}
printf("%s\n", M_STR_OK);
memset(inBuf, 0x00, sizeof(inBuf));
}
return 0;
}
调试
inBuf: 021Abc9000
OK
inBuf: 021Abc9Abc1
i: 3, j: 7,[Abc]
NG
inBuf: 021ABC9000
NG
inBuf: 021$bc9000
OK