Programming Concepts
Programming · 4 question types
A string is a sequence of characters such as "Hello world" or "PASSWORD123". CIE pseudocode names four string operations you must know.
LENGTH
LENGTH(s)returns the number of characters in the strings.
Password ← "letmein"
OUTPUT LENGTH(Password) // outputs 7
Name ← ""
OUTPUT LENGTH(Name) // outputs 0 (empty string)
A common use is validation: rejecting a password that is too short.
INPUT Password
IF LENGTH(Password) < 8
THEN
OUTPUT "Password must be at least 8 characters"
ENDIF
SUBSTRING
SUBSTRING(s, start, length)returns a piece of the strings, beginning at positionstartandlengthcharacters long.
In CIE pseudocode, strings are 1-indexed: the first character is at position 1.
Word ← "Revision"
OUTPUT SUBSTRING(Word, 1, 3) // outputs "Rev"
OUTPUT SUBSTRING(Word, 4, 5) // outputs "ision"
OUTPUT SUBSTRING(Word, 1, 1) // outputs "R"
Useful for extracting parts of formatted data: e.g. taking the first three letters of a postcode, or splitting a date string "05/12/2024" into day, month and year.
UCASE (uppercase)
UCASE(s)returns the stringswith every letter changed to uppercase.
Name ← "Sarah"
OUTPUT UCASE(Name) // outputs "SARAH"
LCASE (lowercase)
LCASE(s)returns the stringswith every letter changed to lowercase.
Name ← "SARAH"
OUTPUT LCASE(Name) // outputs "sarah"
Putting them together
A common idiom: standardise an input before comparing it so that minor differences in case do not cause false rejections.
Writing pseudocode with the string functions
Question: Write pseudocode statements that store a string, extract part of it with SUBSTRING, output it in upper or lower case and output its length (4–6 marks, one per mark point).
Asked in 4 of the 17 papers, twice alongside reading or writing a text file. Each function used correctly is usually a mark point: UCASE(Text) or LCASE(Text) for the case change, LENGTH(Text) for the character count, and SUBSTRING(Text, Start, Number) with the string first, then the start position, then the number of characters. In one scheme SUBSTRING carried three marks: one for the call with the right first parameter, one for the correct start and one for the correct length. Position 1 is the first character and spaces count, so number the characters before you choose a start value. The outputs share one mark at most.
The same functions appear in algorithms you trace or interpret: LENGTH(Entry) < 8 is a minimum-length rule, LCASE(Entry) = Entry is true only when nothing is in upper case, and SUBSTRING(Entry, Index, 1) in a loop tests one character at a time.
Extracting a word and changing its case
Write pseudocode statements that store the string "Practice Makes Perfect" in the variable Phrase, extract and output the word "Makes", output the whole phrase in upper case, and output the number of characters in it.
Solution:
Number the characters from 1, counting the spaces: "Practice" fills positions 1 to 8, the space is position 9, so "Makes" starts at position 10 and is 5 characters long.
DECLARE Phrase : STRING
Phrase ← "Practice Makes Perfect"
OUTPUT SUBSTRING(Phrase, 10, 5)
OUTPUT UCASE(Phrase)
OUTPUT LENGTH(Phrase)
SUBSTRING(Phrase, 10, 5)outputs MakesUCASE(Phrase)outputs PRACTICE MAKES PERFECTLENGTH(Phrase)outputs 22: 8 + 1 + 5 + 1 + 7 characters, the two spaces included
A start of 9 would return " Make" and a start of 11 would return "akes ", so check the count before you commit to it.