9618/22/O/N/25 question 6: worked solution (October/November 2025, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3. Q35 9618/22/O/N/25 original Q6 Structured · 7 marks Worked solution (a) 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 ENDFUNCTION Function 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] KEY IDEA Cut String2 down to the length of String1 (from the start or the end), make the case the same if needed, then compare. TOTAL 7 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by MAK CS. Tap the picture to open it on YouTube.