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.
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.
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
Spaces and punctuation are characters too. LENGTH("Hi there!") is 9, not 7.
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
SUBSTRING("Cambridge", 1, 3) and SUBSTRING("Cambridge", 5, 5) return?The third value is a length, not an end position. Characters 3 to 6 are SUBSTRING(Word, 3, 4), not SUBSTRING(Word, 3, 6).
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What does LENGTH("Hello World") return?
What does SUBSTRING("Keyboard", 4, 5) return?
In the lab, the vowel counter is run with the input Computer. What does it output?
Why does the vowel counter apply LCASE to each letter?
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 CountThe 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)
Without LCASE the capital E at the start of Eleven would not equal 'e', and the program would output 2.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
What does SUBSTRING("Programming", 4, 4) return?
Which expression gives the first character of the string Name?
Code ← "ab12CD", then OUTPUT UCASE(SUBSTRING(Code, 2, 3)). What is output?
Which statement stores the number of characters in Postcode?
This skill is now recorded on your profile. Keep the chain going.