A system requires users to create a password that meets the following rules: The password must be between 8 and 20 characters long The password must contain...

Assessment: Computer Science 0478 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science - 0478

Question 1 Report

A system requires users to create a password that meets the following rules:

  • The password must be between 8 and 20 characters long
  • The password must contain at least one uppercase letter
  • The password must contain at least one digit

(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]

Answer Details

(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
ENDFUNCTION

The 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]

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning