1. 程式人生 > >PAT乙級 1081 檢查密碼 (15 分)

PAT乙級 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.

程式碼:

#include<stdio.h>
#include<string.h>
int main(){
	int N;
	char password[81];
	scanf("%d",&N);
	getchar();
	for(int i=0;i<N;++i){
		gets(password);
		int sign_number=0,sign_letter=0,sign=0,len=strlen(password);
		if(len<6){
			printf("Your password is tai duan le.\n");
		}
		else{
			for(int j=0;j<len;++j){
				if(password[j]>='0'&&password[j]<='9'){
					sign_number=1;
				}
				else if((password[j]>='a'&&password[j]<='z')||(password[j]>='A'&&password[j]<='Z')){
					sign_letter=1;
				}
				else if(password[j]!='.'){
					sign=1;
				}
			}
			if(sign){
				printf("Your password is tai luan le.\n");
			}
			else{
				if(sign_number&&sign_letter){
					printf("Your password is wan mei.\n");
				}
				else if(sign_number==0){
					printf("Your password needs shu zi.\n");
				}
				else if(sign_letter==0){
					printf("Your password needs zi mu.\n");
				}
			}
		}
	}
	return 0;
}

在這裡插入圖片描述