Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 13% of your exam marks.
Paper 2 is almost entirely programming. Variable types, assignments, loops and conditionals are tested every session.
A string is a sequence of characters such as "Hello world" or "PASSWORD123". CIE pseudocode names four string operations you must know.
LENGTH(s)returns the number of characters in the strings.
Password ← "letmein"
OUTPUT LENGTH(Password) // outputs 7
Name ← ""
OUTPUT LENGTH(Name) // outputs 0 (empty string)
A common use is validation: rejecting a password that is too short.
INPUT Password
IF LENGTH(Password) < 8 THEN
OUTPUT "Password must be at least 8 characters"
ENDIF
SUBSTRING(s, start, length)returns a piece of the strings, beginning at positionstartandlengthcharacters long.
In CIE pseudocode, strings are 1-indexed: the first character is at position 1.
Word ← "Revision"
OUTPUT SUBSTRING(Word, 1, 3) // outputs "Rev"
OUTPUT SUBSTRING(Word, 4, 5) // outputs "ision"
OUTPUT SUBSTRING(Word, 1, 1) // outputs "R"
Useful for extracting parts of formatted data: e.g. taking the first three letters of a postcode, or splitting a date string "05/12/2024" into day, month and year.
UCASE(s)returns the stringswith every letter changed to uppercase.
Name ← "Sarah"
OUTPUT UCASE(Name) // outputs "SARAH"
LCASE(s)returns the stringswith every letter changed to lowercase.
Name ← "SARAH"
OUTPUT LCASE(Name) // outputs "sarah"
A common idiom: standardise an input before comparing it so that minor differences in case do not cause false rejections.
Example — A program asks the user to type "yes" to continue. Accept any combination of upper and lower case.
INPUT Answer
IF LCASE(Answer) = "yes" THEN
OUTPUT "Continuing..."
ELSE
OUTPUT "Stopped."
ENDIF
The user can type Yes, YES, yes, or even yEs, and the program will treat them all the same.
Example — The function LENGTH(x) returns the size of a string x. The function SUBSTRING(x, y, z) returns the piece of x that begins at index y and runs for z characters. Positions count from 1.
Write pseudocode that stores "Save my exams" in X, outputs its length, then extracts the word "exams" from the string and outputs it.
X ← "Save my exams"
OUTPUT LENGTH(X) // outputs 13
Y ← 9 // "exams" starts at position 9
Z ← 5 // "exams" is 5 characters long
OUTPUT SUBSTRING(X, Y, Z) // outputs "exams"
Mark-scheme split: 1 mark for storing the string, 1 for calling LENGTH(X), 1 for outputting the length, 1 for using SUBSTRING, 1 for the correct parameters, 1 for outputting the substring.