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]
| Category | Count |
|---|---|
| Uppercase letters | |
| Lowercase letters | |
| Digits | |
| Other characters |
(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: ", OtherCountAll 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:
| Character | Category |
|---|---|
| A | Uppercase |
| b | Lowercase |
| 3 | Digit |
| ! | Other |
| (space) | Other |
| x | Lowercase |
| Y | Uppercase |
| Category | Count |
|---|---|
| Uppercase letters | 2 (A, Y) |
| Lowercase letters | 2 (b, x) |
| Digits | 1 (3) |
| Other characters | 2 (!, space) |
[3]
Everything you need to excel in your exams