16

Computer Science HL Paper 1 (May 2024, TZ2)

  1. String manipulation is a useful programming concept. Strings are zero-indexed: the index of a string’s first character is 0, and the index of a string’s last character is the length of the string minus 1. A set of string manipulation sub-programs is given in Figure 4.
    table of string manipulation sub-programs (subString, lenString, revString, joinString)
    (a) The following section of pseudocode represents an algorithm that is intended to perform validation to check if the length of the string entered is between 10 and 25, inclusive. The algorithm is incorrect and contains two errors.
    input X
    loop while (lenString(X) < 10) AND (lenString(X) > 25)
    output "The string’s length is out of range, please try again"
    output X
    end loop
    Construct the correct pseudocode that removes the two errors. [2]

(b) The following algorithm performs a task using the string sub-programs:
J = ""
loop C from 1 to 4
input X
input Y
input Z
R = subString(X, Y, Z)
S = revString(R)
J = joinString(S, J)
end loop
output J
Copy and complete the trace table for the algorithm using the input data: ADONUS, 3, 2, FERGUS, 0, 3, NASREEN, 1, 4, TUPPENCE, 0, 3, DAMOCLES, 2, 3. The values for columns J, C, X, Y and Z for the first row have been done for you. [5]

(c) Construct this algorithm in pseudocode, making use of the given string manipulation sub-programs. [8]