← IGCSE Computer Science Paper 2

Combining string functions

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.

Understanding Combining string functions in IGCSE Computer Science Paper 2

What it does: SUBSTRING takes 3 characters from position 1, UCASE converts them, and the result is assigned to the STRING variable ShortCode. The innermost function is worked out first. Its result becomes the input of the outer function. LENGTH(Name) - 1 is one character fewer than the whole string. Adding 1 would ask for more characters than Name has. Every opening bracket needs a closing bracket. The nested call needs two closing brackets at the end. LENGTH must be given Name alone, and then 1 is subtracted from the number it returns: LENGTH(Name) - 1.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 347–357) and word search (Modules 337–357).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
In UCASE(SUBSTRING(Name, 1, 1)), which function runs first?
UCASE runs first, then SUBSTRING cuts out the partSUBSTRING runs first, then UCASE changes its result
Matches
UCASE(SUBSTRING(Name, 1, 1)
↓
A closing bracket is missing. Each function call needs its own pair of brackets, so UCASE is never closed.
Applying
Meanings
Term
UCASE(SUBSTRING(Name, 1, 1))
Meaning
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.
Practice
DECLARE Name : STRING
Testing
Gaps
Initial ← UCASE((Name, 1, 1))
Check Answer
Spots
2INPUT Name
3DECLARE I : INTEGER

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Giraffe' badge — High Perspective, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
The innermost function is worked out first. Its result becomes the input of the outer function.
2. Name ← "maya". What does UCASE(SUBSTRING(Name, 1, 1)) give?
A. "MAYA"
B. "M" ✓
SUBSTRING gives "m" (1 character from position 1). UCASE then makes it "M". UCASE only sees the result of SUBSTRING, not the whole of Name.
3. Which expression gives Name without its last character?
A. SUBSTRING(Name, 1, LENGTH(Name) - 1) ✓
B. SUBSTRING(Name, 1, LENGTH(Name) + 1)
LENGTH(Name) - 1 is one character fewer than the whole string. Adding 1 would ask for more characters than Name has.
4. Name ← "Oliver". What does SUBSTRING(Name, 1, LENGTH(Name) - 1) give?
A. "Olive" ✓
B. "Oliver"
LENGTH(Name) is 6, so the expression is SUBSTRING(Name, 1, 5). That gives the first 5 characters: "Olive".
5. Initial ← UCASE(SUBSTRING(Name, 1, 1)) stores a letter. Which declaration is correct?
A. DECLARE Initial : INTEGER
B. DECLARE Initial : STRING ✓
UCASE and SUBSTRING both return text, so the variable that stores the result must be a STRING (or a CHAR if it holds one character). It cannot be an INTEGER.

Match the Pairs

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.
SUBSTRING(Name, 1, LENGTH(Name) - 1) → Gives every character of Name except the last one.
Store the first three letters of Code, in capitals, in a declared STRING variable → ShortCode ← UCASE(SUBSTRING(Code, 1, 3))
Use LENGTH inside SUBSTRING to pick out the last character of Word → SUBSTRING(Word, LENGTH(Word), 1)
UCASE(SUBSTRING(Name, 1, 1) → A closing bracket is missing. Each function call needs its own pair of brackets, so UCASE is never closed.
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.

Key Term

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.

Fill in the Blanks

1. Initial ← UCASE(SUBSTRING(Name, 1, 1))
Hint: Type the inner function that extracts the first character from Name before UCASE changes it.
2. Initial ← UCASE(SUBSTRING(Name, 1, 1))
Hint: Type the outer function that turns the extracted letter into a capital.
3. Short ← SUBSTRING(Name, 1, LENGTH(Name) - 1)
Hint: Type the function that counts the characters in Name, used to work out where to stop.
4. DECLARE Initial : STRING Initial ← UCASE(SUBSTRING(Name, 1, 1))
Hint: Type the data type that suits the text result of a string function.
5. Name ← "Maya" Short ← SUBSTRING(Name, 1, LENGTH(Name) - 1)
Hint: Type the number of characters to leave off the end so that Short holds May.

Spot the Error

Question 1
A DECLARE Name : STRING
B INPUT Name
C DECLARE I : INTEGER
D I ← UCASE(SUBSTRING(Name,1,1))
E OUTPUT I
Line 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.
Question 2
A DECLARE Name : CHAR
B DECLARE I : STRING
C INPUT Name
D I ← UCASE(SUBSTRING(Name,1,1))
E OUTPUT I
Line 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.
Question 3
A INPUT Name
B I ← UCASE(SUBSTRING(1,1,Name))
C OUTPUT I
Line 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).
Question 4
A DECLARE Size : STRING
B Name ← "Jordan"
C Size ← LENGTH(Name)
D Cut ← SUBSTRING(Name,1,Size-1)
E OUTPUT Cut
Line A — LENGTH returns a whole number, so Size must be an INTEGER. It should be DECLARE Size : INTEGER.
Question 5
A INPUT Name
B First ← SUBSTRING(Name, 1, 3)
C First ← UCASE(First)
D OUTPUT First
Line 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).

Student with a class code? Join your class →