Guide 6 of 7

Built-in Operations

Don't reinvent the wheel. Learn how to use Cambridge's built-in library routines to manipulate text strings and generate random numbers.

Python vs. Pseudocode String Methods

Python treats strings as arrays and uses dot-notation (like .upper()). In standard Cambridge pseudocode, you must use explicit built-in functions.

Concept 🐍 Python (Wrong for Exams) 📘 Cambridge Pseudocode (Correct)
Length of a String len(Word) LENGTH(Word)
Uppercase Word.upper() UCASE(Word)
Extraction Word[0:3] SUBSTRING(Word, 1, 3)
Concatenation "Hello " + Name "Hello " & Name

1. String Concatenation

Concatenation is the process of joining two strings together. In Cambridge pseudocode, this is done using the ampersand (&) symbol, not the plus (+) sign.

Concatenation Syntax
DECLARE FirstName : STRING
DECLARE LastName : STRING
DECLARE FullName : STRING

FirstName ← "Alan"
LastName ← "Turing"

// Notice the space added in the middle!
FullName ← FirstName & " " & LastName

2. Substrings and Length

Examiners love asking you to extract data from a string (like pulling the first 3 letters of a surname to generate a username). You use the SUBSTRING(String, StartPos, Length) function for this. Remember, strings are 1-indexed in pseudocode!

Extraction Syntax
DECLARE Word : STRING
DECLARE Code : STRING

Word ← "Computer"

// Extracts 3 characters starting from position 1 ("Com")
Code ← SUBSTRING(Word, 1, 3)

// Gets the length of the string (8)
OUTPUT LENGTH(Word)

3. Math Routines (ROUND & RANDOM)

You don't need to write complex algorithms for basic math. The syllabus provides built-in functions for rounding numbers and generating random values.

ROUND(Identifier, Places): Rounds a REAL number to a specific number of decimal places. e.g., ROUND(3.14159, 2) returns 3.14.
RANDOM(): Generates a random REAL number between 0 and 1. To get a whole number between 1 and 10, you combine it with INT: INT(RANDOM() * 10) + 1.

Interactive Exam Practice

Scenario: Write an algorithm that takes a user's password as input. If the password is less than 8 characters long, output "Too short". Otherwise, output the password in all uppercase letters.

Hover or tap the black box to reveal the examiner's solution. Click Run Code to test it instantly!

Tap to Reveal Answer
DECLARE Password : STRING

OUTPUT "Enter a password:"
INPUT Password

IF LENGTH(Password) < 8 THEN
    OUTPUT "Too short"
ELSE
    OUTPUT UCASE(Password)
ENDIF