PAT~Class B~1081 Check Password ~C++

Topic link: check password

Topic description:

This question requires you to help the user registration module of a website write a small function for checking the validity of passwords. 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 have both letters and numbers.

Input format:

Enter the first line to give a positive integer N (≤ 100), followed by N lines, each line gives a password set by the user, a non-empty string not exceeding 80 characters, terminated by a carriage return.

Output format:

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

  • If the password is valid, output Your password is wan mei.;
  • If the password is too short, output Your password is tai duan le. regardless of whether it is legal or not;
  • If the password length is legal, but there are illegal characters, output Your password is tai luan le.;
  • If the password length is legal, but only letters and no numbers, output Your password needs shu zi.;
  • If the password length is legal, but only numbers without 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.

-------------------------------------------------- --------Answer area---------------------------------------- ------------------

At the beginning, a test point was stuck all the time, but after checking it, some lines of strings contained spaces. If you use cin again, the sentence will be divided into two parts from the space, and you should use getline(cin ,s) this function, and when using this function, you need to use getchar() to eat the newline after the input number. If you don't understand, I wrote a small example below.

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

int main()
{
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        int shuzi_flag=0;
        int zimu_flag=0;
        int luan_flag=0;
        string s;
        getline(cin,s);
        if(s.size()<6)
        {
            printf("Your password is tai duan le.\n");
            continue;
        }
        for(int i=0;i<s.size();i++)
        {
            if(s[i]-'0'>=0&&s[i]-'0'<=9)
            {
                shuzi_flag=1;
            }
            else if((s[i]-'a'<=25&&s[i]-'a'>=0)||(s[i]-'A'<=25&&s[i]-'A'>=0))
            {
                zimu_flag=1;
            }
            else if(s[i]-'.'==0)
            {
                ;
            }
            else
            {
                luan_flag=1;
            }
        }
            if(shuzi_flag==1&&zimu_flag==1&&luan_flag==0)
            {
                printf("Your password is wan mei.\n");
            }
             else if(luan_flag==1)
            {
                printf("Your password is tai luan le.\n");
            }
            else if(shuzi_flag==0&&zimu_flag==0)
            {
                printf("Your password needs shu zi.\n");
                printf("Your password needs zi mu.\n");
            }
            else if(shuzi_flag==0)
            {
                printf("Your password needs shu zi.\n");
            }
            else if(zimu_flag==0)
            {
                printf("Your password needs zi mu.\n");
            }
           
    }
    return 0;
}

Why use getchar()

//getchar() is not used
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string s;
        getline(cin,s);
        cout<<s<<endl;
    }
    return 0;
}
//enter
2
wwww
ssss
//Output Note that there is a blank line

wwww


//use getchar()
int main()
{
    int n;
    cin>>n;
    getchar()
    while(n--)
    {
        string s;
        getline(cin,s);
        cout<<s<<endl;
    }
    return 0;
}
//enter
2
wwww
ssss
//output
wwww
ssss

Welfare area: It is highly recommended that everyone start a book Algorithm Notes , which introduces some skills and knowledge points of brushing questions in detail. After reading it, if it helps, I uploaded the PDF version to my official account (the QR code is in the sidebar at the top left, reply to the algorithm notes ), you can download and take a look if you need it. If there is any infringement, please delete it. It is recommended to start with a physical book.

 

 

 

 

 

Tags: PAT

Posted by pereira2k5 on Sun, 25 Sep 2022 23:37:27 +0530