1081 Password Check (15 points)
This question asks you to help the user registration module of a website write a small function of password validity check. The website requires users to set up passwords which must be composed of not less than 6 characters, and can only contain English letters, numbers and decimal points.
Input format:
Input the first line gives a positive integer N (< 100), followed by N lines, each line gives a user-set password, for no more than 80 characters of non-empty string, to end with carriage return.
Output format:
For each user's password, the system feedback information is output in one line, divided into the following five types:
- If the password is legitimate, output Your password is wan mei.
- If the password is too short, whether legal or not, you password is Tai Duan le.
- If the length of the password is legal, but there are illegal characters, you password is Tai Luan le.
- If the password length is legal, but only letters have no numbers, then output Your password needs shu zi.
- If the password length is legitimate, but only the number has no letters, you password needs Zi mu..
Input sample:
5 123s zheshi.wodepw 1234.5678 WanMei23333 pass*word.6
Output sample:
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.
No matter how simple the question is, Liu Shen will write simpler than me.
Let me put my code first.
#include<iostream> using namespace std; int main(){ int n; cin>>n;getchar(); for(int i=0;i<n;i++){ int hasnum=0; int hasa=0; int no=0; string s; getline(cin,s); if(s.length()<6){ cout<<"Your password is tai duan le."<<endl; continue; }else{ for(int a=0;a<(s.length());a++){ if(s[a]>='a'&&s[a]<='z'){ hasa=1; }else if(s[a]>='A'&&s[a]<='Z'){ hasa=1; }else if(s[a]>='0'&&s[a]<='9'){ hasnum=1; }else if(s[a]=='.'){ } else{ no=1; //cout<<s; //cout<<s[a]<<endl; } } if(no){ printf("Your password is tai luan le.\n"); }else if(hasnum==0){ printf("Your password needs shu zi.\n"); }else if(hasa==0){ printf("Your password needs zi mu.\n"); }else { printf("Your password is wan mei.\n"); } } } return 0; }
Re-release Willow God's
(Fortunately, in fact, it's similar to my nature, but people typesetting is better.)
#include <iostream> #include <cctype> using namespace std; int main() { int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { string s; getline(cin, s); if (s.length() >= 6) { int invalid = 0, hasAlpha = 0, hasNum = 0; for (int j = 0; j < s.length(); j++) { if (s[j] != '.' && !isalnum(s[j])) invalid = 1; else if (isalpha(s[j])) hasAlpha = 1; else if (isdigit(s[j])) hasNum = 1; } if (invalid == 1) cout << "Your password is tai luan le.\n"; else if (hasNum == 0) cout << "Your password needs shu zi.\n"; else if (hasAlpha == 0) cout << "Your password needs zi mu.\n"; else cout << "Your password is wan mei.\n"; } else cout << "Your password is tai duan le.\n"; } return 0; }