In: Computer Science
In python language do thee following:
Some password scheme is designed such that the password must start with a special symbol in “$” “@” or “!” followed by four letters that could be in a..z followed by a similar special symbol followed by three letters in A..Z followed by a single digit. Write a program that takes from the user a string, and verifies whether the string abides by this password scheme. Do this once using regular expressions. Another time without using regular expressions.
Lets see the case where we don't use RegEx. In this case, we need to verify each character of the input password falls in the expected set of characters.
So, we check these characters as we go on traversing the password string.
To check the password characters against the asked character sets, we need to define these sets of Capital and small letters, and special characters as well.
If any of these checks fail, the password is not proper.
CODE:
alphaCapital ="QWERTYUIOPASDFGHJKLZXCVBNM"
alphaSmall = "qwertyuiopasdfghjklzxcvbnm"
splChar = "!@#$%^&*()_"
pwd = input("Enter the password: ")
isCorrect = 1
if pwd[0] not in splChar: #if firstt char is not special
isCorrect=0
print("first spl")
for j in range(1,5):
if pwd[j] not in alphaSmall: #check if not small
isCorrect = 0
print("small alpha not found")
if pwd[5] not in splChar: #checking if not a special char
isCorrect = 0
print("second spl")
for k in range(6,9): #traversing next 3 chars
if pwd[k] not in alphaCapital: #checking if not capital
isCorrect =0
print("Capital not found")
if not pwd[9].isdigit(): #checking if not a digit
isCorrect = 0
print("no digit last")
if isCorrect == 0:
print("Password is not proper!")
else:
print("Password is proper.")
OUTPUT:
Now, less consider the RegEx approach.
In this we will define a pattern as asked in the question.
The pattern will include character classes, their position, and their frequencies in the password string.
We then match the input password against this pattern.
CODE:
import re
pwd = input("Enter the password: ")
#matching the password entered with the regex:
isCorrect = re.search("^[!@#$%^&*()_][a-z]{4}[!@#$%^&*()_][A-Z]{3}[0-9]$", pwd)
if isCorrect:
print("Password is proper!")
else:
print("Password is not proper.")
CODE and OUTPUT: