SUBSTRING: extracting part of a string

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

SUBSTRING: extracting part of a string

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does SUBSTRING("Happy Days", 1, 5) return?
A "Happy"
B "Days"
2. In SUBSTRING("Computer", 1, 3), what does the last number (3) mean?
A How many characters to take
B The position where the extract stops
3. Which call returns "i" from the string "Science"?
A SUBSTRING("Science", 1, 3)
B SUBSTRING("Science", 3, 1)
4. What does SUBSTRING("Pizza", 2, 1) return?
A "iz"
B "i"
5. What is the data type of the result of SUBSTRING("Pizza", 2, 1)?
A STRING, even though it is one character
B CHAR, because it is one character

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SUBSTRING("Happy Days", 1, 5) AReturns "Happy": 5 characters taken from position 1
2SUBSTRING(Word, 3, 1) BPart ← SUBSTRING(Name, 2, 4)
3Take the 4 characters of Name that begin at position 2 and store them in Part CReturns one character: the character at position 3 of Word, as a string
4Output the first character of Name by extracting one character from the start DOUTPUT SUBSTRING(Name, 1, 1)
5SUBSTRING("Happy Days", 5) ESUBSTRING always returns a string, so it cannot be assigned to an INTEGER variable.
6Letter: INTEGER Letter ← SUBSTRING(Word, 1, 1) FThe length argument is missing. SUBSTRING needs three arguments: the string, the start and the length.

Definitions

SUBSTRING(<string>, <start>, <length>)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Part ← _________("Happy Days", 1, 5)
2.
OUTPUT SUBSTRING("Happy Days", _, 4)
3.
Name ← "Happy Days" Part ← SUBSTRING(Name, 1, _)
4.
Letter ← SUBSTRING("Happy Days", 7, _)
5.
DECLARE Part : ______ Part ← SUBSTRING("Happy Days", 1, 5)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Word : STRING
B DECLARE Part : STRING
C Word ← "Happy Days"
D Part ← SUBSTRING(Word, 5, 1)
E OUTPUT Part
Question 2
A DECLARE Letter : STRING
B Name ← "Cambridge"
C Letter ← SUBSTRING(Name, 0, 1)
D OUTPUT Letter
Question 3
A Text ← "IGCSE 0478"
B Code ← SUBSTRING[Text, 7, 4]
C OUTPUT Code
Question 4
A Word ← "Computer"
B Part ← SUBSTRING(Word, 1, -3)
C OUTPUT Part
Question 5
A DECLARE Word : STRING
B DECLARE Part : INTEGER
C Word ← "Happy Days"
D Part ← SUBSTRING(Word, 7, 4)
E OUTPUT Part

Answer Key

SUBSTRING: extracting part of a string
Section A: Quick Questions
Q1
A — "Happy"
Q2
A — How many characters to take
Q3
B — SUBSTRING("Science", 3, 1)
Q4
B — "i"
Q5
A — STRING, even though it is one character
Section B: Match the Pairs
Q1
SUBSTRING("Happy Days", 1, 5) → Returns "Happy": 5 characters taken from position 1
Q2
SUBSTRING(Word, 3, 1) → Returns one character: the character at position 3 of Word, as a string
Q3
Take the 4 characters of Name that begin at position 2 and store them in Part → Part ← SUBSTRING(Name, 2, 4)
Q4
Output the first character of Name by extracting one character from the start → OUTPUT SUBSTRING(Name, 1, 1)
Q5
SUBSTRING("Happy Days", 5) → The length argument is missing. SUBSTRING needs three arguments: the string, the start and the length.
Q6
Letter: INTEGER Letter ← SUBSTRING(Word, 1, 1) → SUBSTRING always returns a string, so it cannot be assigned to an INTEGER variable.
Section C: Definitions
Q1
SUBSTRING(<string>, <start>, <length>) — Returns part of a string: length characters taken from the start position. SUBSTRING("Happy Days", 1, 5) gives "Happy". The result is always a string.
Section D: Fill in the Blanks
Q1
Part ← SUBSTRING("Happy Days", 1, 5)
Q2
OUTPUT SUBSTRING("Happy Days", 7, 4)
Q3
Name ← "Happy Days" Part ← SUBSTRING(Name, 1, 5)
Q4
Letter ← SUBSTRING("Happy Days", 7, 1)
Q5
DECLARE Part : STRING Part ← SUBSTRING("Happy Days", 1, 5)
Section E: Spot the Error
Q1
D — The start and length are swapped. This takes 1 character from position 5, which gives "y". To get "Happy" it must start at 1 and take 5 characters: SUBSTRING(Word, 1, 5).
Q2
C — Positions in Cambridge pseudocode start at 1, so a start of 0 is not valid. The first character is taken with SUBSTRING(Name, 1, 1).
Q3
B — SUBSTRING is a function, so its arguments go inside round brackets ( ), not square brackets. It should be SUBSTRING(Text, 7, 4).
Q4
B — The length must be a positive integer because you cannot take a negative number of characters. It should be SUBSTRING(Word, 1, 3), which gives "Com".
Q5
B — SUBSTRING always returns a string, so the variable that stores the result must be declared as STRING, not INTEGER.