Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.
Check the lengths first. If String2 is shorter, return FALSE. If case does not matter, change both strings to upper case. Then cut String2 down to the length of String1, from the left for 's' or from the right for 'e', and compare.
FUNCTION Compare(String1, String2 : STRING, CaseMatters : BOOLEAN, Position : CHAR) RETURNS BOOLEAN
DECLARE S1Length : INTEGER
S1Length ← LENGTH(String1)
IF LENGTH(String2) < S1Length THEN
RETURN FALSE
ENDIF
IF CaseMatters = FALSE THEN
String1 ← TO_UPPER(String1)
String2 ← TO_UPPER(String2)
ENDIF
CASE OF Position
'e' : String2 ← RIGHT(String2, S1Length)
's' : String2 ← LEFT(String2, S1Length)
ENDCASE
IF String1 = String2 THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTIONFunction heading with all four parameters, return type and ending [1]
Length of String1 found [1]
Return FALSE if String2 is shorter than String1 [1]
The (changed) strings are compared [1]
CaseMatters tested and, if FALSE, both strings changed to the same case [1]
's' and 'e' tested and the matching part of String2 taken (LEFT or RIGHT) [1]
Correct BOOLEAN returned in every case [1]
▶ Whole paper
Video solution of the whole paper · by MAK CS. 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.