A program needs to analyse a string entered by the user and count the number of uppercase letters, lowercase letters, digits and other characters. (a) Write...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

A program needs to analyse a string entered by the user and count the number of uppercase letters, lowercase letters, digits and other characters.

(a) Write pseudocode for this algorithm. The program should output the count for each category. [5]

(b) Complete the table showing the expected output for the input "Ab3! xY". [3]

CategoryCount
Uppercase letters
Lowercase letters
Digits
Other characters

Answer Details

(a) The algorithm examines each character in the string and classifies it into one of four categories using ASCII range checks. The nested IF structure ensures each character is counted in exactly one category:

DECLARE Text : STRING
DECLARE UpperCount : INTEGER
DECLARE LowerCount : INTEGER
DECLARE DigitCount : INTEGER
DECLARE OtherCount : INTEGER
DECLARE i : INTEGER
DECLARE Ch : CHAR

INPUT Text
UpperCount <- 0
LowerCount <- 0
DigitCount <- 0
OtherCount <- 0

FOR i <- 1 TO LENGTH(Text)
    Ch <- Text[i]
    IF Ch >= 'A' AND Ch <= 'Z' THEN
        UpperCount <- UpperCount + 1
    ELSE
        IF Ch >= 'a' AND Ch <= 'z' THEN
            LowerCount <- LowerCount + 1
        ELSE
            IF Ch >= '0' AND Ch <= '9' THEN
                DigitCount <- DigitCount + 1
            ELSE
                OtherCount <- OtherCount + 1
            ENDIF
        ENDIF
    ENDIF
NEXT i

OUTPUT "Uppercase: ", UpperCount
OUTPUT "Lowercase: ", LowerCount
OUTPUT "Digits: ", DigitCount
OUTPUT "Other: ", OtherCount

All counters are initialised to 0 [1]. The loop iterates through each character [1]. The uppercase check uses 'A' to 'Z' [1]. Lowercase and digit checks are in the ELSE branches so no character is double-counted [1]. Everything that is not a letter or digit falls into "Other" [1]. [5]

(b) Analysing "Ab3! xY" character by character:

CharacterCategory
AUppercase
bLowercase
3Digit
!Other
(space)Other
xLowercase
YUppercase
CategoryCount
Uppercase letters2 (A, Y)
Lowercase letters2 (b, x)
Digits1 (3)
Other characters2 (!, space)

[3]

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