SUBSTRING function

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

SUBSTRING function

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 "Happy "
2. In Cambridge pseudocode, which position is the first character of a string?
A Position 1
B Position 0
3. In SUBSTRING("Computer", 4, 3), what does the 3 mean?
A The position to stop at
B The number of characters to return
4. What does SUBSTRING("Computer", 4, 3) return?
A "put"
B "mpu"
5. Which line picks out only the first character of Code?
A SUBSTRING(Code, 1, 0)
B SUBSTRING(Code, 1, 1)

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) AIF SUBSTRING(ID, 1, 1) = "A" THEN
2SUBSTRING(Word, 3, 1) BReturns just the third character of the string stored in Word
3Take the first 3 characters of a string called Code and store them in Prefix CPrefix ← SUBSTRING(Code, 1, 3)
4A format check that tests whether the first character of ID is the letter A DReturns "Happy": 5 characters starting at position 1, the first character
5SUBSTRING("Happy Days", 0, 5) EThe length is the number of characters to take, not an end position. Starting at 7 with length 10 runs past the end of the string
6SUBSTRING("Happy Days", 7, 10) FPosition 0 does not exist because the first character is at position 1, so the start value must be 1

Definitions

SUBSTRING("Happy Days", 1, 5)

Fill in the Blanks

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

1.
_________("Happy Days", 1, 5)
2.
SUBSTRING("Happy Days", _, 5)
3.
Word ← SUBSTRING("Happy Days", 7, _)
4.
Initial ← SUBSTRING(Name, 1, _) OUTPUT Initial
5.
IF SUBSTRING(ID, 4, 1) __ "-" THEN OUTPUT "Invalid format" ENDIF

Spot the Error

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

Question 1
A Word ← "Happy Days"
B First ← SUBSTRING(Word, 0, 5)
C OUTPUT First
Question 2
A Letter ← SUBSTRING("Cloud", 3)
B OUTPUT Letter
C // Outputs o
Question 3
A DECLARE Code : STRING
B Code ← SUBSTRING(3, 3, "AB123")
C OUTPUT Code
Question 4
A INPUT Name
B Initial ← SUBSTRING(Name, 1, 1)
C OUTPUT "Initial"
Question 5
A Word ← "Computer"
B Total ← LENGTH(Word)
C FOR I ← 1 TO Total
D Ch ← SUBSTRING(Word, 1, I)
E OUTPUT Ch
F NEXT I

Answer Key

SUBSTRING function
Section A: Quick Questions
Q1
A — "Happy"
Q2
A — Position 1
Q3
B — The number of characters to return
Q4
A — "put"
Q5
B — SUBSTRING(Code, 1, 1)
Section B: Match the Pairs
Q1
SUBSTRING("Happy Days", 1, 5) → Returns "Happy": 5 characters starting at position 1, the first character
Q2
SUBSTRING(Word, 3, 1) → Returns just the third character of the string stored in Word
Q3
Take the first 3 characters of a string called Code and store them in Prefix → Prefix ← SUBSTRING(Code, 1, 3)
Q4
A format check that tests whether the first character of ID is the letter A → IF SUBSTRING(ID, 1, 1) = "A" THEN
Q5
SUBSTRING("Happy Days", 0, 5) → Position 0 does not exist because the first character is at position 1, so the start value must be 1
Q6
SUBSTRING("Happy Days", 7, 10) → The length is the number of characters to take, not an end position. Starting at 7 with length 10 runs past the end of the string
Section C: Definitions
Q1
SUBSTRING("Happy Days", 1, 5) — SUBSTRING(string, start, length) returns length characters beginning at position start. Position 1 is the first character, so this returns "Happy".
Section D: Fill in the Blanks
Q1
SUBSTRING("Happy Days", 1, 5)
Q2
SUBSTRING("Happy Days", 1, 5)
Q3
Word ← SUBSTRING("Happy Days", 7, 4)
Q4
Initial ← SUBSTRING(Name, 1, 1) OUTPUT Initial
Q5
IF SUBSTRING(ID, 4, 1) <> "-" THEN OUTPUT "Invalid format" ENDIF
Section E: Spot the Error
Q1
B — In SUBSTRING the first character is at position 1, so a start of 0 is invalid. The line should be First ← SUBSTRING(Word, 1, 5), which gives "Happy".
Q2
A — SUBSTRING needs three arguments: the string, the start and the length. The length is missing. It should be SUBSTRING("Cloud", 3, 1) to pick out the single character "o".
Q3
B — The arguments are in the wrong order. SUBSTRING takes the string first, then the start position, then the length: SUBSTRING("AB123", 3, 3), which returns "123".
Q4
C — Putting Initial in quotes outputs the word Initial instead of the value stored in the variable. It should be OUTPUT Initial, with no quotes.
Q5
D — The start and length are swapped. To pick out one character at a time, the start must be I and the length must be 1: SUBSTRING(Word, I, 1).