Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Slice the string.

Names, passwords, postcodes and messages are all strings. Learn the four string operations in the 0478 syllabus: finding the length, taking a substring, and converting to upper or lower case.

The key idea

A string is a sequence of characters. LENGTH(s) returns the number of characters, spaces included. SUBSTRING(s, start, length) returns length characters starting at position start. UCASE and LCASE return the string in upper or lower case. In 0478 pseudocode the first character is generally at position 1; many programming languages, including Python, start at 0.

the string "Happy Days": positions start at 1 12345 678910 Happy · Days space start 7, length 4 SUBSTRING("Happy Days", 7, 4) = "Days" LENGTH("Happy Days") = 10
Fig. 1: SUBSTRING takes a start position and a number of characters
Section 01

Length and case.

In the lab, choose the String handling program. It uses all four operations to count the vowels in a word; step through it and watch each letter being taken out with SUBSTRING.

DECLARE Name : STRING
Name ← "Ada Lovelace"
OUTPUT LENGTH(Name)
OUTPUT UCASE(Name)
OUTPUT LCASE(Name)

This outputs 12, ADA LOVELACE and ada lovelace. The space counts as a character, so LENGTH is 3 + 1 + 8 = 12.

The same idea in Python (for comparison only: the exam uses pseudocode)

name = "Ada Lovelace"
print(len(name))      # 12
print(name.upper())   # ADA LOVELACE
print(name.lower())   # ada lovelace
Examiner trap

Spaces and punctuation are characters too. LENGTH("Hi there!") is 9, not 7.

Section 02

SUBSTRING.

SUBSTRING needs three things: the string, the position to start at, and how many characters to take.

DECLARE Word : STRING
Word ← "COMPUTER"
OUTPUT SUBSTRING(Word, 4, 3)
OUTPUT SUBSTRING(Word, LENGTH(Word), 1)

The same idea in Python (for comparison only: the exam uses pseudocode)

word = "COMPUTER"
print(word[3:6])      # PUT: Python counts from 0 and stops before 6
print(word[-1])       # R
Worked example Counting positions
What is output by the pseudocode above?
  1. Number the characters: C1 O2 M3 P4 U5 T6 E7 R8.
  2. SUBSTRING(Word, 4, 3) starts at position 4 and takes 3 characters: P, U, T.
  3. LENGTH(Word) is 8, so SUBSTRING(Word, 8, 1) is the last character.
Answer: PUT, then R
Your turn
What do SUBSTRING("Cambridge", 1, 3) and SUBSTRING("Cambridge", 5, 5) return?
Examiner trap

The third value is a length, not an end position. Characters 3 to 6 are SUBSTRING(Word, 3, 4), not SUBSTRING(Word, 3, 6).

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What does LENGTH("Hello World") return?

Quick check+10 XP

What does SUBSTRING("Keyboard", 4, 5) return?

Quick check+10 XP

In the lab, the vowel counter is run with the input Computer. What does it output?

Quick check+10 XP

Why does the vowel counter apply LCASE to each letter?

Section 03

Strings in a loop.

To look at every character, loop from 1 to LENGTH and take a substring of length 1 each time.

DECLARE Word : STRING
DECLARE Count : INTEGER
DECLARE Index : INTEGER
DECLARE Letter : CHAR
INPUT Word
Count ← 0
FOR Index ← 1 TO LENGTH(Word)
    Letter ← LCASE(SUBSTRING(Word, Index, 1))
    IF Letter = 'e'
      THEN
        Count ← Count + 1
    ENDIF
NEXT Index
OUTPUT Count

The same idea in Python (for comparison only: the exam uses pseudocode)

word = input()
count = 0
for letter in word.lower():
    if letter == "e":
        count = count + 1
print(count)
Worked example Dry run
The program above is run with the input Eleven. What is output?
  1. LENGTH("Eleven") is 6, so Index runs from 1 to 6.
  2. The letters after LCASE are e, l, e, v, e, n.
  3. Count goes up at positions 1, 3 and 5.
Answer: 3
Your turn
What is output for the input Excellence?
Examiner trap

Without LCASE the capital E at the start of Eleven would not equal 'e', and the program would output 2.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What does SUBSTRING("Programming", 4, 4) return?

Exam style+20 XP

Which expression gives the first character of the string Name?

Exam style+20 XP

Code ← "ab12CD", then OUTPUT UCASE(SUBSTRING(Code, 2, 3)). What is output?

Exam style+20 XP

Which statement stores the number of characters in Postcode?

Skill unlocked

String handling, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
LENGTH, SUBSTRING, UCASE and LCASE: working out results and writing string-processing loops.