Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  String handling
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

String handling

Slice the string. Names, passwords, postcodes and messages are all strings.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 4 of 11 in Programming; follows Iteration: count-controlled, pre-condition and post-condition loops; leads into Arithmetic, relational and logical operators
Central model
Fig. 1: SUBSTRING takes a start position and a number of characters
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Length and case: In the lab, choose the String handling program.
  • SUBSTRING: SUBSTRING needs three things: the string, the position to start at, and how many characters to take.
  • Strings in a loop: To look at every character, loop from 1 to LENGTH and take a substring of length 1 each time.
Key points to leave on the board
  • Write positions under each character, starting at 1. Spaces count.
  • SUBSTRING(s, start, length): the third value is how many characters, not where to stop.
  • Case: convert with UCASE or LCASE before comparing.
  • Every character: FOR Index ← 1 TO LENGTH(s) with SUBSTRING(s, Index, 1).
The core visual model

One picture every idea returns to

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
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Slice the string.. Names, passwords, postcodes and messages are all strings. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Length and caseIn the lab, choose the String handling program. Project tab 5 of the lab (String handling) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: SUBSTRINGSUBSTRING needs three things: the string, the position to start at, and how many characters to take. Model the worked example Counting positions on the board, one line at a time. Your turn: What do SUBSTRING("Cambridge", 1, 3) and SUBSTRING("Cambridge", 5, 5) return?Pairs
21 to 30 minTeach and check: Strings in a loopTo look at every character, loop from 1 to LENGTH and take a substring of length 1 each time. Model the worked example Dry run on the board, one line at a time. Your turn: What is output for the input Excellence?Corners
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Strings in a loop, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Corners

Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Spaces and punctuation are characters too.LENGTH("Hi there!") is 9, not 7.
The third value is a length, not an end position.Characters 3 to 6 are SUBSTRING(Word, 3, 4), not SUBSTRING(Word, 3, 6).
Without LCASE the capital E at the start of Eleven would not equal 'e', and the program would output 2.Return to Strings in a loop: ask a pair to state the correct version and show it on the lab before moving on.
Worksheet

String handling: worksheet

6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[2 marks]

State the purpose of the string operations LENGTH and SUBSTRING.

Q2 · trace[3 marks]

Text ← "Data Science". State what is returned by (a) LENGTH(Text) (b) SUBSTRING(Text, 6, 3) (c) UCASE(SUBSTRING(Text, 1, 4)).

Q3 · write[4 marks]

Write pseudocode that inputs a person's first name and outputs its first letter in upper case and the number of letters in the name.

Q4 · write[4 marks]

Write pseudocode that inputs a sentence and outputs the number of words, assuming words are separated by single spaces.

Q5 · explain[2 marks]

A login program compares the username typed with the stored username "SMITH". A user types smith and is rejected. Explain why and how to fix it.

Q6 · analysis[2 marks]

A student wants characters 3 to 6 of Word and writes SUBSTRING(Word, 3, 6). Explain the error and correct it.

Answer key and marking notes
Q1 · 2 marks
  • LENGTH returns the number of characters in a string.
  • SUBSTRING returns part of a string: a given number of characters starting at a given position.
Q2 · 3 marks
  • (a) 12 (4 + 1 space + 7)
  • (b) "Sci"
  • (c) "DATA"
Q3 · 4 marks
  • INPUT FirstName
  • Initial ← UCASE(SUBSTRING(FirstName, 1, 1))
  • OUTPUT Initial
  • OUTPUT LENGTH(FirstName)
Q4 · 4 marks
  • Spaces ← 0 and INPUT Sentence
  • FOR Index ← 1 TO LENGTH(Sentence)
  • IF SUBSTRING(Sentence, Index, 1) = " " THEN Spaces ← Spaces + 1
  • After the loop: OUTPUT Spaces + 1
Q5 · 2 marks
  • String comparison is case sensitive: "smith" is not equal to "SMITH".
  • Convert the input with UCASE (or both strings to the same case) before comparing.
Q6 · 2 marks
  • The third parameter is the number of characters, so this returns 6 characters (positions 3 to 8).
  • Correct: SUBSTRING(Word, 3, 4).
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

What is the value of LENGTH("Top Score 10")?

  • A12
  • B10
  • C9
  • D11
Q21 mark

Word holds "KEYBOARD". What is output by OUTPUT SUBSTRING(Word, 4, 3)?

  • ABOA
  • BOAR
  • CYBO
  • DBOARD
Q31 mark

Which expression gives the last character of the string variable Word?

  • ASUBSTRING(Word, LENGTH(Word), 1)
  • BSUBSTRING(Word, 1, LENGTH(Word))
  • CSUBSTRING(Word, LENGTH(Word) - 1, 1)
  • DLENGTH(SUBSTRING(Word, 1, 1))
Q41 mark

The input is Alabama. What is output?

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

  • A3
  • B4
  • C7
  • D2
Q51 mark

Name holds "mARIE". What is output?

OUTPUT UCASE(SUBSTRING(Name, 1, 1)), LCASE(SUBSTRING(Name, 2, LENGTH(Name) - 1))

  • AMarie
  • BmARIE
  • CMARIE
  • DMmarie
Q61 mark

This program is meant to count the words in a sentence by counting the spaces.

DECLARE Sentence : STRING
DECLARE Words : INTEGER
DECLARE Index : INTEGER
INPUT Sentence
Words ← 1
FOR Index ← 1 TO LENGTH(Sentence)
    IF SUBSTRING(Sentence, Index, 1) = ' '
      THEN
        Words ← Words + 1
    ENDIF
NEXT Index
OUTPUT Words
Which input shows that the program does not always give the right answer?

  • AHi  there (two spaces between the words)
  • BHello
  • CGood morning all
  • Da b c d
Quiz key
Q1: A

T-o-p (3) + space (1) + S-c-o-r-e (5) + space (1) + 1-0 (2) = 12 characters. 10 leaves out the two spaces. 9 counts only the letters. 11 misses one of the spaces or one of the digits.

Q2: A

Counting from position 1, K-E-Y-B, so position 4 is B; taking 3 characters gives BOA. OAR counts from position 0, as Python does. YBO starts one place too early. BOARD takes every character from position 4 to the end instead of just 3.

Q3: A

With positions starting at 1, the last character is at position LENGTH(Word), and taking 1 character from there gives it. SUBSTRING(Word, 1, LENGTH(Word)) returns the whole word. LENGTH(Word) - 1 is the second-to-last position. LENGTH(SUBSTRING(Word, 1, 1)) is the number 1, not a character.

Q4: A

Alabama has the letters A, l, a, b, a, m, a. The comparison is with lower-case 'a', and a capital A is a different character, so only the three lower-case a's are counted: 3. 4 would need LCASE around the SUBSTRING. 7 is the length of the word. 2 misses the final character, as if the loop stopped at LENGTH(Word) - 1.

Q5: A

SUBSTRING(Name, 1, 1) is m, and UCASE makes it M. LENGTH(Name) - 1 is 4, so SUBSTRING(Name, 2, 4) is ARIE, and LCASE makes it arie. Together: Marie. mARIE is the original string unchanged. MARIE would need UCASE on the whole name. Mmarie wrongly starts the second part at position 1.

Q6: A

Hi, two spaces, there has 2 words but 2 spaces, so the program outputs 3. Hello has no spaces and gives 1, which is right. Good morning all has 2 spaces and gives 3, which is right. a b c d has 3 spaces and gives 4, which is right; single letters are still words.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonIteration: count-controlled, pre-condition and post-condition loops