Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
If the title has 16 characters or fewer, return it unchanged. Otherwise remove words from the end until the title is shorter than 14 characters, then add three dots.
FUNCTION Trim(Name : STRING) RETURNS STRING
CONSTANT Dots = "..."
CONSTANT Space = " "
IF LENGTH(Name) <= 16 THEN
RETURN Name
ENDIF
WHILE LENGTH(Name) > 13
REPEAT
Name ← LEFT(Name, LENGTH(Name) - 1)
UNTIL RIGHT(Name, 1) = Space
ENDWHILE
Name ← LEFT(Name, LENGTH(Name) - 1)
Name ← Name & Dots
RETURN Name
ENDFUNCTIONFunction heading, parameter, return type and ending [1]
If the length is 16 or less, the original title is returned [1]
A conditional loop that repeats until the string is short enough (less than 14) [1]
An inner loop (or second condition) that finds the word to remove from the end [1]
Characters are stripped back to a space [1]
The space in front of the removed word is also removed [1]
The three dots are joined on and the result is returned [1]
Write: the file becomes very large, because redundant zeros are stored [1]
Write one of these: put a separator (delimiter) character between the values, or make the first character show the length of each sample [1]
Write: the algorithm to store and extract the samples is more complex and slower to run [1]
▶ Whole paper
Video solution of the whole paper · by Pseudocode Pro. 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.