Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Use a CharCount array of six elements: 1 to 5 for a, e, i, o, u and 6 for the other letters. Set all to 0, convert each character to lower case, use CASE to add 1 to the right count, then output all six.
PROCEDURE CountVowels(ThisString : STRING)
DECLARE Index : INTEGER
DECLARE ThisChar : CHAR
FOR Index ← 1 TO 6
CharCount[Index] ← 0
NEXT Index
FOR Index ← 1 TO LENGTH(ThisString)
ThisChar ← LCASE(MID(ThisString, Index, 1))
CASE OF ThisChar
'a' : CharCount[1] ← CharCount[1] + 1
'e' : CharCount[2] ← CharCount[2] + 1
'i' : CharCount[3] ← CharCount[3] + 1
'o' : CharCount[4] ← CharCount[4] + 1
'u' : CharCount[5] ← CharCount[5] + 1
'a' TO 'z' : CharCount[6] ← CharCount[6] + 1
ENDCASE
NEXT Index
FOR Index ← 1 TO 6
OUTPUT CharCount[Index]
NEXT Index
ENDPROCEDUREProcedure heading with the string parameter, and ENDPROCEDURE [1]
Local variable declared for the loop counter (CharCount is not declared, as it is global) [1]
All elements of CharCount set to 0 [1]
Loop through every character of ThisString [1]
MID() used to take out a single character [1]
Each vowel tested (upper or lower case) and its count increased [1]
Other letters tested and the consonant count increased [1]
Results output after the loop [1]
▶ Whole paper
Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.