前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Basic Level) Practice (中文)1081 检查密码

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

作者头像
C you again 的博客
发布2020-09-15 10:53:24
5580
发布2020-09-15 10:53:24
举报
文章被收录于专栏:IT技术圈(CSDN)
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. 判断是否只有 数字、字母和 小数点组成
代码语言:javascript
复制
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. 判断是否只有数字或者只有字母
代码语言:javascript
复制
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、输出

代码:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/02/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1081 检查密码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档