Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3.
Use RIGHT to cut off the front, then LEFT to keep only the characters needed.
FUNCTION MID(InString : STRING, Start, Num : INTEGER) RETURNS STRING
DECLARE MidString : STRING
DECLARE InStringLen : INTEGER
InStringLen ← LENGTH(InString)
MidString ← RIGHT(InString, InStringLen - Start + 1)
MidString ← LEFT(MidString, Num)
RETURN MidString
ENDFUNCTIONFunction heading and ending with parameters and return type [1]
Correct use of one of the substring functions [1]
Correct use of both functions in the right order [1]
Return the substring [1]
Using LEFT first (length Start + Num - 1) and then RIGHT (length Num) also scores.
Any two checks:
Check that Start and Num are 1 or more (positive) [1]
Check that the string is long enough for the operation (Start + Num - 1 is not more than its length) [1]
▶ Whole paper
Video solution of the whole paper · by Awab Aqib. 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.