Combining string functions

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

Combining string functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In UCASE(SUBSTRING(Name, 1, 1)), which function runs first?
A SUBSTRING runs first, then UCASE changes its result
B UCASE runs first, then SUBSTRING cuts out the part
2. Name ← "maya". What does UCASE(SUBSTRING(Name, 1, 1)) give?
A "MAYA"
B "M"
3. Which expression gives Name without its last character?
A SUBSTRING(Name, 1, LENGTH(Name) - 1)
B SUBSTRING(Name, 1, LENGTH(Name) + 1)
4. Name ← "Oliver". What does SUBSTRING(Name, 1, LENGTH(Name) - 1) give?
A "Olive"
B "Oliver"
5. Initial ← UCASE(SUBSTRING(Name, 1, 1)) stores a letter. Which declaration is correct?
A DECLARE Initial : INTEGER
B DECLARE Initial : STRING

Match the Pairs

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

Left columnRight column
1UCASE(SUBSTRING(Name, 1, 1)) AShortCode ← UCASE(SUBSTRING(Code, 1, 3))
2SUBSTRING(Name, 1, LENGTH(Name) - 1) BGives the first letter of Name in capitals, such as an initial. SUBSTRING runs first, then UCASE changes its result.
3Store the first three letters of Code, in capitals, in a declared STRING variable CSUBSTRING(Word, LENGTH(Word), 1)
4Use LENGTH inside SUBSTRING to pick out the last character of Word DA closing bracket is missing. Each function call needs its own pair of brackets, so UCASE is never closed.
5UCASE(SUBSTRING(Name, 1, 1) EGives every character of Name except the last one.
6SUBSTRING(Name, 1, LENGTH(Name - 1)) FThe - 1 is inside LENGTH, so it tries to subtract 1 from a string. It should be outside the LENGTH brackets.

Definitions

UCASE(SUBSTRING(Name, 1, 1))

Fill in the Blanks

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

1.
Initial ← UCASE(_________(Name, 1, 1))
2.
Initial ← _____(SUBSTRING(Name, 1, 1))
3.
Short ← SUBSTRING(Name, 1, ______(Name) - 1)
4.
DECLARE Initial : ______ Initial ← UCASE(SUBSTRING(Name, 1, 1))
5.
Name ← "Maya" Short ← SUBSTRING(Name, 1, LENGTH(Name) - _)

Spot the Error

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

Question 1
A DECLARE Name : STRING
B INPUT Name
C DECLARE I : INTEGER
D I ← UCASE(SUBSTRING(Name,1,1))
E OUTPUT I
Question 2
A DECLARE Name : CHAR
B DECLARE I : STRING
C INPUT Name
D I ← UCASE(SUBSTRING(Name,1,1))
E OUTPUT I
Question 3
A INPUT Name
B I ← UCASE(SUBSTRING(1,1,Name))
C OUTPUT I
Question 4
A DECLARE Size : STRING
B Name ← "Jordan"
C Size ← LENGTH(Name)
D Cut ← SUBSTRING(Name,1,Size-1)
E OUTPUT Cut
Question 5
A INPUT Name
B First ← SUBSTRING(Name, 1, 3)
C First ← UCASE(First)
D OUTPUT First

Answer Key

Combining string functions
Section A: Quick Questions
Q1
A — SUBSTRING runs first, then UCASE changes its result
Q2
B — "M"
Q3
A — SUBSTRING(Name, 1, LENGTH(Name) - 1)
Q4
A — "Olive"
Q5
B — DECLARE Initial : STRING
Section B: Match the Pairs
Q1
UCASE(SUBSTRING(Name, 1, 1)) → Gives the first letter of Name in capitals, such as an initial. SUBSTRING runs first, then UCASE changes its result.
Q2
SUBSTRING(Name, 1, LENGTH(Name) - 1) → Gives every character of Name except the last one.
Q3
Store the first three letters of Code, in capitals, in a declared STRING variable → ShortCode ← UCASE(SUBSTRING(Code, 1, 3))
Q4
Use LENGTH inside SUBSTRING to pick out the last character of Word → SUBSTRING(Word, LENGTH(Word), 1)
Q5
UCASE(SUBSTRING(Name, 1, 1) → A closing bracket is missing. Each function call needs its own pair of brackets, so UCASE is never closed.
Q6
SUBSTRING(Name, 1, LENGTH(Name - 1)) → The - 1 is inside LENGTH, so it tries to subtract 1 from a string. It should be outside the LENGTH brackets.
Section C: Definitions
Q1
UCASE(SUBSTRING(Name, 1, 1)) — Nesting string functions: the inner SUBSTRING runs first, then UCASE changes its result. This gives the first letter of Name in capitals, such as an initial.
Section D: Fill in the Blanks
Q1
Initial ← UCASE(SUBSTRING(Name, 1, 1))
Q2
Initial ← UCASE(SUBSTRING(Name, 1, 1))
Q3
Short ← SUBSTRING(Name, 1, LENGTH(Name) - 1)
Q4
DECLARE Initial : STRING Initial ← UCASE(SUBSTRING(Name, 1, 1))
Q5
Name ← "Maya" Short ← SUBSTRING(Name, 1, LENGTH(Name) - 1)
Section E: Spot the Error
Q1
C — UCASE(SUBSTRING(...)) returns a string, so the variable that stores the initial must be declared as STRING, not INTEGER. The line should be DECLARE I : STRING.
Q2
A — A CHAR holds only one character, but Name stores a whole name that SUBSTRING must take a part of. It should be DECLARE Name : STRING.
Q3
B — SUBSTRING takes the string first, then the start position, then the length. The arguments are in the wrong order. It should be SUBSTRING(Name,1,1).
Q4
A — LENGTH returns a whole number, so Size must be an INTEGER. It should be DECLARE Size : INTEGER.
Q5
B — The third argument is the number of characters to take. To get a single initial it must be 1, not 3. The line should be First ← SUBSTRING(Name, 1, 1).