检查密码格式

题目:检查密码是否符合规定

密码要求:

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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值