Operators and Expressions · Cambridge syllabus 8.1 · Lesson 4 of 4
String Handling and Library Routines
String handling uses four functions: LENGTH(S) gives the number of characters, SUBSTRING(S, Start, Length) gives part of the string, and UCASE and LCASE change the case. In 0478 pseudocode the first character is usually position 1, but some languages count from 0, so a question will say which. The library routines are MOD and DIV, ROUND(Value, Places) to round a value or an expression to a number of decimal places, and RANDOM(), which returns a random number between 0 and 1.
By the end, you can
- Use LENGTH to find the number of characters in a string.
- Use SUBSTRING to extract part of a string.
- Use UCASE and LCASE to change the case of a string or character.
- Understand that the first character of a string can be position 0 or 1.
- Use MOD and DIV as library routines.
- Use ROUND to round a value or an expression to a given number of decimal places.
- Use RANDOM to generate a random number.
String handling
Programs often need to work with text: checking the length of a password, taking the first letters of a name, or making a reply match whatever case the user typed. The syllabus names four string operations, written as functions.
LENGTH(Text) returns the number of characters in a string, counting spaces. LENGTH("Happy Days") is 10. It is how a length check is written: IF LENGTH(Password) < 8 rejects a short password.
SUBSTRING(Text, Start, Length) returns part of a string: Length characters, starting at position Start. With the first character at position 1, SUBSTRING("Happy Days", 1, 5) is "Happy" and SUBSTRING("Happy Days", 7, 4) is "Days". The third value is how many characters to take, not where to stop. The syllabus allows the first character to be position 0 or position 1, and a question will say which; in 0478 pseudocode it is generally 1.
UCASE(Text) returns the string with every letter in upper case, and LCASE(Text) returns it in lower case. UCASE("Happy") is "HAPPY" and LCASE('W') is 'w'; they work on a single character as well as a string. They are used to compare text without worrying about case: IF UCASE(Answer) = "YES" accepts yes, Yes and YES.
These can be combined. With Name ← "amira", UCASE(SUBSTRING(Name, 1, 1)) is "A", the capital initial, and Initial ← SUBSTRING(Name, 1, 1) stores it. Store each result in a variable when it will be used again.
Worth remembering
- LENGTH(Text) is the number of characters, including spaces.
- SUBSTRING(Text, Start, Length) takes Length characters from position Start.
- UCASE and LCASE change the case of a string or a character.
- The first character can be position 0 or 1; in 0478 pseudocode it is generally 1.
Library routines
A library routine is a ready-made function that comes with the language, so the programmer can use it without writing it. The syllabus names four: MOD, DIV, ROUND and RANDOM.
MOD and DIV, met in the arithmetic lesson, work on integers. DIV(10, 3) returns 3, the whole number of times 3 goes into 10. MOD(10, 3) returns 1, the remainder.
ROUND(Value, Places) returns a value rounded to a number of decimal places. The value can be an identifier or an expression: ROUND(Price, 2) rounds a variable to two places, and ROUND(Total / Count, 1) rounds an average straight away. ROUND(7.456, 1) is 7.5, and ROUND(7.456, 0) is 7.
RANDOM() returns a random number between 0 and 1 inclusive. To get a random whole number in a range, multiply and round: ROUND(RANDOM() * 6, 0) gives a whole number from 0 to 6, and ROUND(RANDOM() * 5, 0) + 1 gives one from 1 to 6, like a dice roll. Because the result is different each time, a program that uses RANDOM cannot be traced to one fixed answer; a question will ask for the possible range instead.
Worth remembering
- Library routines are ready-made: MOD, DIV, ROUND and RANDOM.
- ROUND(Value, Places) rounds an identifier or an expression.
- RANDOM() gives a number from 0 to 1; multiply and round for a whole number.
Worked example
With Word ← "Computer": LENGTH(Word) is 8, SUBSTRING(Word, 1, 4) is "Comp", UCASE(Word) is "COMPUTER". ROUND(7 / 3, 2) is 2.33, and ROUND(RANDOM() * 6, 0) gives a whole number from 0 to 6.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
"mbri": four characters starting at the third, m.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.