LENGTH: number of characters in a string

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

LENGTH: number of characters in a string

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does LENGTH("Happy Days") return?
A 10
B 9
2. Which data type does LENGTH return?
A STRING
B INTEGER
3. What does LENGTH("") return?
A 0
B 1
4. Which line correctly stores the length of Name in a variable?
A Size ← LENGTH(Name)
B Size ← LENGTH Name
5. Does LENGTH("Hi there") count the space?
A No, it returns 7
B Yes, it returns 8

Match the Pairs

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

Left columnRight column
1LENGTH("Happy Days") AName ← "Sam Lee" OUTPUT LENGTH(Name)
2LENGTH("") BSpaces do count as characters, so LENGTH("Hi there") is 8. Do not subtract 1 for the space.
3LENGTH gives a whole number, so its result can be stored in an INTEGER variable CDECLARE Size : INTEGER Size ← LENGTH("Cambridge")
4LENGTH counts the characters of the string held in a variable DThe string literal has no quotation marks. It should be LENGTH("Happy Days").
5Size ← LENGTH(Happy Days) EReturns 10, because the space between the words counts as a character
6OUTPUT LENGTH("Hi there") - 1 // student expects 7 because spaces do not count FReturns 0, because an empty string has no characters

Definitions

LENGTH(Text)

Fill in the Blanks

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

1.
Size ← ______("Happy Days")
2.
DECLARE Total : _______ Total ← LENGTH("Happy Days") OUTPUT Total
3.
Count ← LENGTH("") OUTPUT Count // Output: _
4.
Name ← "Ava Lee" Size ← LENGTH(____) OUTPUT Size
5.
Size _ LENGTH("Happy Days") OUTPUT Size // Output: 10

Spot the Error

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

Question 1
A DECLARE Word : STRING
B DECLARE Count : STRING
C Word ← "Happy Days"
D Count ← LENGTH(Word)
Question 2
A DECLARE Name : STRING
B DECLARE Size : INTEGER
C Name ← "Happy Days"
D Size ← LEN(Name)
Question 3
A Word ← "Hi there"
B Size ← LENGTH(Word)
C OUTPUT "Characters: " Size
Question 4
A Phrase ← "Happy Days"
B // spaces count too
C Size ← LENGTH(Phrase) - 1
D OUTPUT Size
Question 5
A Empty ← ""
B Size ← LENGTH("Empty")
C OUTPUT Size

Answer Key

LENGTH: number of characters in a string
Section A: Quick Questions
Q1
A — 10
Q2
B — INTEGER
Q3
A — 0
Q4
A — Size ← LENGTH(Name)
Q5
B — Yes, it returns 8
Section B: Match the Pairs
Q1
LENGTH("Happy Days") → Returns 10, because the space between the words counts as a character
Q2
LENGTH("") → Returns 0, because an empty string has no characters
Q3
LENGTH gives a whole number, so its result can be stored in an INTEGER variable → DECLARE Size : INTEGER Size ← LENGTH("Cambridge")
Q4
LENGTH counts the characters of the string held in a variable → Name ← "Sam Lee" OUTPUT LENGTH(Name)
Q5
Size ← LENGTH(Happy Days) → The string literal has no quotation marks. It should be LENGTH("Happy Days").
Q6
OUTPUT LENGTH("Hi there") - 1 // student expects 7 because spaces do not count → Spaces do count as characters, so LENGTH("Hi there") is 8. Do not subtract 1 for the space.
Section C: Definitions
Q1
LENGTH(Text) — Returns an INTEGER: the number of characters in the string, including spaces. LENGTH("Happy Days") returns 10 and LENGTH("") returns 0.
Section D: Fill in the Blanks
Q1
Size ← LENGTH("Happy Days")
Q2
DECLARE Total : INTEGER Total ← LENGTH("Happy Days") OUTPUT Total
Q3
Count ← LENGTH("") OUTPUT Count // Output: 0
Q4
Name ← "Ava Lee" Size ← LENGTH(Name) OUTPUT Size
Q5
Size ← LENGTH("Happy Days") OUTPUT Size // Output: 10
Section E: Spot the Error
Q1
B — LENGTH returns a whole number, so the variable that stores the result must be declared as INTEGER, not STRING.
Q2
D — The Cambridge pseudocode function is called LENGTH, not LEN. It should be Size ← LENGTH(Name).
Q3
C — Values in an OUTPUT statement must be separated by commas. It should be OUTPUT "Characters: ", Size.
Q4
C — Spaces count as characters, so LENGTH("Happy Days") is already 10. Subtracting 1 gives the wrong answer; the line should be Size ← LENGTH(Phrase).
Q5
B — The quotes turn Empty into a string literal of 5 characters. To measure the empty string stored in the variable, use LENGTH(Empty), which gives 0.