Cambridge O Level Computer Science 2210 · 8.1 Programming concepts

Q21 · 2210/22/M/J/23 Q11

Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 1 of 3.

Q212210/22/M/J/23original Q11Structured · 7 marks
Question 11 from 2210/22/M/J/23

Worked solution

(a)

DECLARE P : STRING
DECLARE Q : CHAR
P ← "The world"
Q ← 'W'

Any two correct lines [2]

(b)

Change P to upper case, then check each character in turn until Q is found.

P ← UCASE(P)
Counter ← 1
Position ← 0
REPEAT
    IF SUBSTRING(P, Counter, 1) = Q
        THEN
            Position ← Counter
    ENDIF
    Counter ← Counter + 1
UNTIL Position <> 0 OR Counter > LENGTH(P)

P converted to upper case with UCASE [1]

Length of P found with LENGTH [1]

Loop that checks each position in turn [1]

SUBSTRING used and the counter stored in Position when found [1]

Other correct loops score too.

(c)

P becomes THE WORLD. T = 1, H = 2, E = 3, space = 4, W = 5.

Write: 5 [1]

KEY IDEALoop through the string one character at a time with SUBSTRING and keep the counter when the character matches.
TOTAL 7 marks

Official mark scheme

Mark scheme for 2210/22/M/J/23 question 11
VIDEO
▶ Whole paper

Video solution of the whole paper · by CS BY AQIB KHAN. 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.