PAT-B: 1081 Check password (15 points)



1. Topic

This question requires you to help the user registration module of a certain website write a small function to check the validity of the password. The website requires that the password set by the user must consist of no less than 6 characters, and can only contain English letters, numbers and decimal points. It must also contain both letters and numbers.

Input format:

Input the first line to give a positive integer N (≤ 100), and then N lines, each line gives a user-set password, which is a non-empty string of no more than 80 characters, and ends with a carriage return.

Output format:

For each user's password, output system feedback information in one line, divided into the following 5 types:

  1. If the password is legal, output Your password is wan mei.;
  2. If the password is too short, whether it is legal or not, it will output Your password is tai duan le.;
  3. If the length of the password is legal, but there are illegal characters, the output Your password is tai luan le.;
  4. If the password length is legal, but there are only letters but no numbers, output Your password needs shu zi.;
  5. If the length of the password is legal, but there are only numbers and no letters, output Your password needs zi mu..

Input sample:

5
123S
zheshi.wodepw
1234.5678
WanMei23333
Pass * word.6

Sample output:

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.


2. Ideas and precautions

  1. Test point 2 needs to read spaces, and string can be used to store strings
  2. In #include "cctype", there is a method "isdigit()" to determine whether it is a number and a method "isalpha()" to determine whether it is an English letter
  3. First judge whether the length meets the requirements, if not, use "continue" to enter the next cycle.

The defined function for judging character length is as follows:

bool judge_len(string str)
{
    
    
	if(str.length()<6) return false;
	else return true;
}


Three, AC code

#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdio>
using namespace std;

bool judge_len(string str);

int main()
{
    
    
	int N;
	string str;
	cin>>N;
	char a=getchar();                    //吸收回车键,没有这个键会把	5当成第一串字符读进去 
	for(int i=0;i<N;i++)
	{
    
    	
		int flag_al =0;					//字符统计
		int flag_dig=0;					//数字统计
		int flag_ill=0;					//非法字母 
		getline(cin,str);
		if(judge_len(str)==0)	
		{
    
    
			cout<<"Your password is tai duan le."<<endl;
			continue;
		}
		else 
		{
    
    
			for(int i=0;i<str.length();i++)
			{
    
    
				if(isalpha(str[i]))			
				{
    
    
					flag_al=1;
				}									
				else if (isdigit(str[i])) 		
				{
    
    
					flag_dig=1;
			 	}							
				else if(!(isalpha(str[i]))&& !( isdigit(str[i]) ) && (str[i]!='.') ) 
				{
    
    
					flag_ill=1;
				}
		    }
		    //判断
			if(flag_ill==1) 				 
			{
    
    		
					cout<<"Your password is tai luan le."<<endl;	
					continue;
			}
		    else if(flag_al==1&&flag_dig==0) 
			{
    
    
					cout<<"Your password needs shu zi."<<endl;		
					continue;
			}	    
		    else if(flag_al==0&&flag_dig==1) 
			{
    
    			
					cout<<"Your password needs zi mu."<<endl;			
					continue;	
			}
		    else 
		    {
    
    
		    	if(flag_al==1&&flag_dig==1&&flag_ill==0)
		    	{
    
      
		    		cout<<"Your password is wan mei."<<endl;
		    		continue;
				}
			}
		}
	}
}

bool judge_len(string str)
{
    
    
	if(str.length()<6) return false;
	else return true;
}

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/114006222