1. 程式人生 > >PAT-B-1081

PAT-B-1081

clas swd scanf passwd png IT AI color 註冊

1081. 檢查密碼 (15)

本題要求你幫助某網站的用戶註冊模塊寫一個密碼合法性檢查的小功能。該網站要求用戶設置的密碼必須由不少於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.

本來是想多用幾個函數來判斷,突然想到c語言中有判斷是否為數字字母的函數,直接上代碼:
#include<stdio.h>
#include<ctype.h>
#include<string.h>

#define P 100;
#define MAX 80;

void is_ok(char *passwd,int length){
    //用於記錄字母,數字,小數點的數量 
    int letter=0,number=0,point=0;
    int i;
    for(i=0;i<length;i++){
        //判斷字符中是否含有非法字符,一遍走 
        if(passwd[i] == . || isalnum(passwd[i])){
            
if(isalpha(passwd[i])){ letter += 1; }else if(isdigit(passwd[i])){ number += 1; } }else{ printf("Your password is tai luan le.\n"); return ; } } if(letter == 0){ printf("Your password needs zi mu.\n"); }else if( number == 0){ printf("Your password needs shu zi.\n"); }else{ printf("Your password is wan mei.\n"); } } int main(){ int n,j,passLen; char passwd[80]; scanf("%d",&n); for(j=0;j<n;j++){ scanf("%s",passwd); passLen = strlen(passwd); if(passLen < 6){ printf("Your password is tai duan le.\n"); }else{ is_ok(passwd,passLen); } } return 0; }

但是有一個測試點沒有通過,希望大佬能夠提醒一下

技術分享圖片


PAT-B-1081