TopMyGrade

GCSE/Computer Science/AQA

CS2.7String handling: length, position, substring, concatenation, conversion to upper/lower case and to ASCII values

Notes

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 exampleWorked 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 exampleWorked 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 mistakesPitfalls

  1. Off-by-one in SUBSTRING — AQA's endpoints are inclusive. SUBSTRING(0, 4, "HELLO") returns the whole string.
  2. Case-sensitive comparison — "Hello" != "hello". Convert to common case first.
  3. Confusing LEN with last index — last index is LEN(s) - 1, not LEN(s).
  4. Concatenating int and string — convert with INT_TO_STRING first to avoid type errors.
  5. Negative position when not found — handle gracefully if your function returns -1.

Try thisQuick check

Given s = "Computer Science":

  • LEN(s) = 16
  • POSITION(s, 'S') = 9
  • SUBSTRING(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

Practice questions

Try each before peeking at the worked solution.

  1. Question 13 marks

    LEN and SUBSTRING

    Given `SET s TO "PROGRAMMING"`, state the value of:
    

    (a) LEN(s)
    (b) SUBSTRING(0, 6, s)
    (c) SUBSTRING(7, 10, s)

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 23 marks

    POSITION function

    Given `SET s TO "COMPUTER"`, state the value returned by POSITION(s, 'P') and POSITION(s, 'Z').
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 34 marks

    Case-insensitive comparison

    Write pseudocode that reads a username from the user and decides whether it equals "admin" regardless of case.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 44 marks

    ASCII arithmetic

    Given that ASC('A') = 65 and that lowercase letters are 32 higher than their uppercase equivalents, write the value of ASC('m') and explain how to convert 'm' to 'M' using arithmetic on ASCII codes.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 53 marks

    Concatenation

    A pupil writes:

    SET age TO 14
    OUTPUT "I am " + age + " years old"
    

    In a strictly-typed language this errors. Explain and fix.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 65 marks

    Reverse a string

    Write pseudocode that reads a string from the user and outputs it reversed.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Trace string operations

    Trace the output of this pseudocode:

    SET s TO "ABCDE"
    SET t TO SUBSTRING(1, 3, s)
    SET u TO TO_LOWER(t)
    OUTPUT u + s
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.7 — String handling

11-card SR deck for AQA GCSE Computer Science topic CS2.7

11 cards · spaced repetition (SM-2)