
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.
Given ‘STR’ = “Codingninja#1” As it satisfies all the above conditions so it is a valid password and therefore you have to return true.
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.
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.
1 <= T <= 5
1 <= |STR| <= 5*10^3
Time Limit: 1 second
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:
If str[i] is equal to ‘ ‘(space)
'CONTAIN_SPACE’ = 1;
Check for all above ASCII code and change the corresponding variable.
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:
{‘CONTAIN_SPACE’ = 1;}