String handling
Strings are sequences of characters. They turn up everywhere — names, postcodes, file paths, sentences entered by a user. AQA's GCSE spec lists a handful of string operations you must be fluent with: length, position, substring, concatenation, case conversion and conversion to/from ASCII codes.
Indexing — zero or one?
AQA pseudocode uses zero-based indexing: the first character is at position 0. So in s = "HELLO", s[0] is 'H' and s[4] is 'O'. Many real languages (Python, Java) also start at 0, but pseudocode questions sometimes vary — read the question carefully.
Length
SET s TO "computer"
SET n TO LEN(s) -- n = 8
Length counts every character including spaces and punctuation. LEN("Hi there!") = 9.
Position (find a character)
SET pos TO POSITION("HELLO", 'L') -- 2 (the first L)
Returns -1 (or some sentinel) if the character is not present — read the spec for the exact convention. AQA's reference uses POSITION(s, c) returning the zero-based index of the first occurrence.
Substring (slice a section)
SET sub TO SUBSTRING(1, 3, "HELLO") -- "ELL"
AQA's pseudocode is SUBSTRING(start, end, s) — both endpoints inclusive, zero-indexed. Reading right-to-left helps:
- Start at position 1 (E)
- End at position 3 (L)
- Result: positions 1, 2, 3 → "ELL"
Concatenation (join strings)
The + operator joins strings:
SET first TO "Hard"
SET second TO "ware"
SET word TO first + second -- "Hardware"
You can mix with other types but expect to convert numbers to strings first:
OUTPUT "Score: " + INT_TO_STRING(87)
Case conversion
SET upper TO TO_UPPER("hello") -- "HELLO"
SET lower TO TO_LOWER("HELLO") -- "hello"
Useful for case-insensitive comparison: convert both strings to lowercase before comparing.
ASCII conversion
Each character has an integer ASCII code (0–127 for standard ASCII):
- 'A' = 65, 'B' = 66, …, 'Z' = 90
- 'a' = 97, 'b' = 98, …, 'z' = 122
- '0' = 48, '1' = 49, …, '9' = 57
Note the gap of 32 between an uppercase and lowercase letter — you can convert case by adding/subtracting 32.
SET code TO ASC('A') -- 65
SET ch TO CHR(66) -- 'B'
The 32-bit gap means CHR(ASC('a') - 32) = 'A'.
✦Worked example— Worked example — count vowels
SET s TO "Hello World"
SET count TO 0
FOR i FROM 0 TO LEN(s) - 1
SET ch TO TO_LOWER(SUBSTRING(i, i, s))
IF ch = 'a' OR ch = 'e' OR ch = 'i' OR ch = 'o' OR ch = 'u' THEN
SET count TO count + 1
ENDIF
ENDFOR
OUTPUT count -- 3
✦Worked example— Worked example — reverse a string
SET s TO "ALGORITHM"
SET reversed TO ""
FOR i FROM LEN(s) - 1 TO 0 STEP -1
SET reversed TO reversed + SUBSTRING(i, i, s)
ENDFOR
OUTPUT reversed -- "MHTIROGLA"
⚠Common mistakes— Pitfalls
- Off-by-one in SUBSTRING — AQA's endpoints are inclusive. SUBSTRING(0, 4, "HELLO") returns the whole string.
- Case-sensitive comparison — "Hello" != "hello". Convert to common case first.
- Confusing LEN with last index — last index is LEN(s) - 1, not LEN(s).
- Concatenating int and string — convert with INT_TO_STRING first to avoid type errors.
- Negative position when not found — handle gracefully if your function returns -1.
➜Try this— Quick check
Given s = "Computer Science":
LEN(s)= 16POSITION(s, 'S')= 9SUBSTRING(0, 7, s)= "Computer"TO_UPPER(s)= "COMPUTER SCIENCE"ASC('C') - ASC('A')= 2 (C is 2 positions after A)
AI-generated · claude-opus-4-7 · v3-deep-computer-science