PAT (Basic Level) Practice (中文)1081 检查密码

时间:2022-07-24
本文章向大家介绍PAT (Basic Level) Practice (中文)1081 检查密码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1081 检查密码

本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。

输入格式:

输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。

输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下5种:

  • 如果密码合法,输出 Your password is wan mei. ;
  • 如果密码太短,不论合法与否,都输出 Your password is tai duan le. ;
  • 如果密码长度合法,但存在不合法字符,则输出 Your password is tai luan le. ;
  • 如果密码长度合法,但只有字母没有数字,则输出 Your password needs shu zi. ;
  • 如果密码长度合法,但只有数字没有字母,则输出 Your password needs zi mu.。

输入样例:

5 123s zheshi.wodepw 1234.5678 WanMei23333 pass*word.6

输出样例:

Your password is tai duan le. Your password needs shu zi. Your password needs zi mu. Your password is wan mei. Your password is tai luan le.

分析:

1、计算输入字符串的长度,判断是否满足条件(>=6) 2、对于长度满足条件的字符串

  1. 判断是否只有 数字、字母和 小数点组成
char str[101][88];
int fun1(int i,int len)
{
    int j;
    int l=1;
    for(j=0;j<len;j++)
    {
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
           ||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
        else
        {
            l=0;
            break;
        }
    }
    return l;
}
  1. 判断是否只有数字或者只有字母
int fun2(int i,int len)
{
    int j;
    int temp1=0;
    int temp2=0;
    for(j=0;j<len;j++)
    {
        if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
    }
    if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
    if(temp1==0&&temp2>0) return 0;  //只有字母,没有数字
    if(temp1>0&&temp2>0) return 1;  //既有数字,又有字母
}

3、输出

代码:

#include<stdio.h>
char str[101][88];
int fun1(int i,int len)
{
    int j;
    int l=1;
    for(j=0;j<len;j++)
    {
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
           ||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
        else
        {
            l=0;
            break;
        }
    }
    return l;
}

int fun2(int i,int len)
{
    int j;
    int temp1=0;
    int temp2=0;
    for(j=0;j<len;j++)
    {
        if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
    }
    if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
    if(temp1==0&&temp2>0) return 0;  //只有字母,没有数字
    if(temp1>0&&temp2>0) return 1;  //既有数字,又有字母
}
int main()
{
    int i;
    int N;
    scanf("%d",&N);
    getchar();  //吸收回车
    for(i=0;i<N;i++)
        gets(str[i]);
    for(i=0;i<N;i++)
    {
        int len=strlen(str[i]);
       // printf("len==%d ",len);
        if(len>=6)
        {
          int temp1=fun1(i,len);
          if(temp1==1)
          {
             int temp2=fun2(i,len);
             if(temp2==-1) printf("Your password needs zi mu.n");
             if(temp2==0) printf("Your password needs shu zi.n");
             if(temp2==1) printf("Your password is wan mei.n");
          }
          else printf("Your password is tai luan le.n");
        }
        else
        {
            printf("Your password is tai duan le.n");
        }
    }
    return 0;
}