Last Updated: 17 Mar, 2021

Program to check the validity of a Password

Easy
Asked in companies
UHG314e Corporation

Problem statement

Ninjas are trying to hack a system of a terrorist organization so that they can know where they will be going to attack next. But to hack the system and to get access to data they need a password that must satisfy certain conditions as described below:

1) Length of the password must be between 8 to 15 characters.
2) At least one digit (0-9), one lowercase letter (a-z), one uppercase letter (A-Z) and one special character (%, ^, &, #, *, %, etc) must be present.
3) Password must not contain any space.

You are given a string ‘STR’, help ninjas to find whether it's a valid password or not.

For example :

Given ‘STR’ =  “Codingninja#1” As it satisfies all the above conditions so it is a valid password and therefore you have to return true.

Input Format

The first line of input contains an integer 'T' representing the number of test cases.

The description of the next ‘T’ lines is as follows-.

The first and the only line of each test case contains a single string 'STR' representing the password.

Output Format:

For each test case, return “Valid” if the given string ‘STR’ satisfies all the above conditions and “Not Valid” if it's not. 

The output of each test case will be printed in a separate line.

Constraints:

1 <= T <= 5
1 <= |STR| <= 5*10^3

Time Limit: 1 second

Approaches

01 Approach

The idea here is we will check all conditions one by one, if any condition fails at any instance we will directly return false and output Not valid password else we will output valid.

Every character has a particular number known as ASCII code so we will use the ASCII code to check whether all conditions are satisfied or not.

In the below table there are ASCII codes given of the required characters.

 

Ascii Code    Characters
48 - 57    Digits (0-9)
65 - 90    Uppercase (A-Z)
97 - 122    Lowercase (a-z)
33 - 47, 58 - 64, 91 - 96, 123 - 126    Special Characters

 

For a valid password, we need to check at least one character from each row must be present.

Algorithm:

  • Declare 6 variables ‘N’= STR.length, ‘CONTAIN_DIGIT’ = 0, ‘CONTAIN_UPPER’ = 0, ‘CONTAIN_LOWER’ = 0, ‘CONTAIN_SPECIAL’ = 0, ‘CONTAIN_SPACE’ = 0.
  • Check if ‘N’ is between 8 and 15. If it's not then return false.
  • Run a loop from i = 0 to ‘N’. Here ‘N’ is the length of the string.

             If str[i] is equal to  ‘ ‘(space)   

                  'CONTAIN_SPACE’ = 1;

Check for all above ASCII code and change the corresponding variable.

 

  • Return true if(‘CONTAIN_SPACE’ = 0 and ‘CONTAIN_DIGIT’ = 1 and ‘CONTAIN_UPPER’ = 1 and ‘CONTAIN_LOWER’ = 1 and ‘CONTAIN_SPECIAL’ = 1) else return false.

02 Approach

In this approach, we will use the find function to find a particular character if the character is present then it will return its position else it will return the end of the position of string(npos).

First, we will check the length of the password and then one by one all characters.

If all the characters present at least once then we will return true else false.

 

Algorithm:

  • Declare 6 variables ‘N’= STR.length, ‘CONTAIN_DIGIT’ = 0, ‘CONTAIN_UPPER’ = 0, ‘CONTAIN_LOWER’ = 0, ‘CONTAIN_SPECIAL’ = 0, ‘CONTAIN_SPACE’ = 0 and a string ‘SPECIAL_CHARACTERS’ = “~`!@#$%^&*()_-+=}]{[|\”’:;?/>.<,”.
  • First, check if ‘N’ is between 8 and 15. If ‘N’ is not between 8 and 15 then return false.
  • If ( STR.find(“ “) )

{‘CONTAIN_SPACE’ = 1;}

  • Run a loop from i = “0” to i = “9” and then from “A” to “Z” and “a” to “z”
    • If ‘STR[i]’ is digit then we make ’CONTAIN_DIGIT’ = 1 similarly for the upper case and lower case characters.
  • Run a loop from i = 0 to i = ‘SPECIAL_CHARACTERS’.length
    • if( STR.find(‘SPECIAL_CHARACTERS’[i]) ){‘CONTAIN_SPECIAL’ = 1;break;}
  • Return true if(‘CONTAIN_SPACE’ = 0 and ‘CONTAIN_DIGIT’ = 1 and ‘CONTAIN_UPPER’ = 1 and ‘CONTAIN_LOWER’ = 1 and ‘CONTAIN_SPECIAL’ = 1) else return false