Question 1 Report
A system requires users to create a password that meets the following rules:
(a) Write pseudocode for a function called CheckPassword that takes a string parameter and returns TRUE if the password meets all three rules, or FALSE otherwise. [5]
(a) The function checks three password rules: length between 8 and 20, at least one uppercase letter, and at least one digit. It loops through each character, setting flags when an uppercase letter or digit is found:
FUNCTION CheckPassword(Password : STRING) RETURNS BOOLEAN
DECLARE Length : INTEGER
DECLARE HasUpper : BOOLEAN
DECLARE HasDigit : BOOLEAN
DECLARE i : INTEGER
DECLARE Ch : CHAR
Length <- LENGTH(Password)
IF Length < 8 OR Length > 20 THEN
RETURN FALSE
ENDIF
HasUpper <- FALSE
HasDigit <- FALSE
FOR i <- 1 TO Length
Ch <- Password[i]
IF Ch >= 'A' AND Ch <= 'Z' THEN
HasUpper <- TRUE
ENDIF
IF Ch >= '0' AND Ch <= '9' THEN
HasDigit <- TRUE
ENDIF
NEXT i
IF HasUpper = TRUE AND HasDigit = TRUE THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTIONThe length check provides an early exit for passwords that are too short or too long. The loop sets Boolean flags for each required character type, and the final check ensures both flags are TRUE. [5]
Everything you need to excel in your exams