Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1411203
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-21 22:00:07

目描述:
输入一组字符串,如果字符串(1)长度没超过8,(2)由大写字母、小写字母、数字、其他字符最少三中组成,(3)含有2个以上的相同子串则判定为NG,否则为OK
例如:
021Abc9000 --》OK
代码:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.   
  5. #define MAXLEN 50
  6. #define N 10

  7. int CodeType(char* str)
  8. {
  9.     int count[4]={0};
  10.     char* pStr;
  11.     int i;
  12.     int num = 0;
  13.     pStr = str;
  14.     while (*pStr != '\0')
  15.     {
  16.         if (*pStr>='A' && *pStr<='Z')
  17.         {
  18.             count[0]++;
  19.         }
  20.         else if (*pStr>='a' && *pStr<='z')
  21.         {
  22.             count[1]++;
  23.         }
  24.         else if (*pStr>='0' && *pStr<='9')
  25.         {
  26.             count[2]++;
  27.         }
  28.         else
  29.         {
  30.             count[3]++;
  31.         }
  32.         pStr++;
  33.     }
  34.     for (i=0; i<4; i++)
  35.     {
  36.         if (count[i] != 0)
  37.         {
  38.             ++num;
  39.         }
  40.     }
  41.     return num;
  42. }

  43. int SameSub(char* str)
  44. {
  45.     int len;
  46.     int i;
  47.     int j;
  48.     len = strlen(str);
  49.     for (i=0; i<=len-3; i++)
  50.     {
  51.         for (j=i+1; j<=len-3; j++)
  52.         {
  53.             if ( (strncmp((const char*)(str+i),(const char*)(str+j),3)) == 0 )
  54.             {
  55.                 return 0;
  56.             }
  57.         }
  58.     }
  59.     return -1;
  60. }
  61. void Check(char* str)
  62. {
  63.     int len;
  64.     int type;
  65.     if (NULL == str)
  66.     {
  67.         printf("NG\n");
  68.         return ;
  69.     }
  70. //length check
  71.     len = strlen(str);
  72.     if (len<=8)
  73.     {
  74.         printf("NG\n");
  75.         return ;    
  76.     }
  77. //data type check
  78.     type = CodeType(str);
  79.     if (type<3)
  80.     {
  81.         printf("NG\n");
  82.         return ;    
  83.     }
  84. //same substring check
  85.     if (SameSub(str) == 0)
  86.     {
  87.         printf("NG\n");
  88.         return ;    
  89.     }
  90.     
  91.     printf("OK\n");
  92.     return ;
  93. }
  94. int main()
  95. {
  96.     char str[N][MAXLEN]={0};
  97.     int num = 0;
  98.     int i;
  99.     while (scanf("%s",str[num]) != EOF)
  100.     {
  101.         num++;
  102.     }
  103.     for (i=0; i<num; i++)
  104.     {
  105.         Check(str[i]);        
  106.     }
  107. }
阅读(1270) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~