Skip to topic
    ← Back to course topics

    String-handling operations in a programming language — AQA A-Level Computer Science

    String-handling operations in AQA A-Level Computer Science encompass the manipulation, extraction, and inspection of textual data using operations such as length, concatenation, substring, and character-code conversion. Mastering these standard algorithms and functions is vital for solving both Paper 1 on-screen programming challenges and Paper 2 algorithmic design questions.

    String-handling operations in a programming language explained

    The position statement in string handling refers to finding where one string first occurs inside another.

    Read the full explanation

    Many languages provide a function or method that returns the index at which a search string begins within a target string, and this index is the position. Indexing conventions matter: some languages count from 0 and others from 1, and the function may return a sentinel value such as -1 when the search string is absent. For example, if the target is "computer" and the search string is "put", a zero-based position returns 3, because c is index 0, o is 1, m is 2 and p is 3. Students should be able to state the indexing convention used, predict the returned value for a given example, and explain how the result can be used to decide whether a substring exists before extracting part of the string.

    What to demonstrate

    1. Explains that position returns the starting index of a search string within a target string.
    2. States that the indexing convention (zero-based or one-based) must be identified before the result is interpreted.
    3. Predicts the correct returned value for a worked example using the stated convention.
    Show all 5 objectives
    1. Identifies the value returned when the search string is not found, such as -1 or a language-specific equivalent.
    2. Links the position result to a conditional check before performing further string operations.

    String-handling operations in a programming language exam tips

    Topic Overview

    String-handling operations provide the algorithmic foundation for processing, parsing, and validating textual input in computer software. In AQA A-Level Computer Science, students must understand string traversal, slicing, conversion to and from character codes, concatenation, and search functions across pseudo-code and high-level programming languages.

    These operations link abstract data representations with concrete program execution, forming a critical component of both the Paper 1 Skeleton Program analysis and Paper 2 algorithmic design questions. Proficiency in manipulating string buffers and character arrays is essential for data sanitisation, protocol parsing, and cryptographic implementation.

    Key Concepts
    • →String indexing and slicing: accessing individual characters via indices and extracting contiguous substrings using start positions and length or end boundaries.
    • →Concatenation and interpolation: joining multiple strings or data types into a single unified textual sequence.
    • →Character encoding conversion: mapping individual characters to their numeric representations (ASCII/Unicode) using functions such as ASC/ORD and CHR.
    • →String inspection methods: determining sequence length, finding target substring positions, case conversion, and evaluating character properties.
    Marking Points
    • Explains that position returns the starting index of a search string within a target string.
    • States that the indexing convention (zero-based or one-based) must be identified before the result is interpreted.
    • Predicts the correct returned value for a worked example using the stated convention.
    • Identifies the value returned when the search string is not found, such as -1 or a language-specific equivalent.
    • Links the position result to a conditional check before performing further string operations.
    Examiner Tips
    • 💡Always write out the indexing of the target string character by character before answering a position question.
    • 💡State the convention you are using (zero-based or one-based) explicitly in your answer.
    • 💡When asked how position might be used, describe a concrete check such as testing whether the result is not equal to the not-found value.
    • 💡In Paper 1 on-screen exams, always sanitise string inputs using strip/trim and upper/lower functions before performing equality checks to prevent false negatives from whitespace or casing.
    • 💡When tracing pseudo-code, write out index tables directly above string literals to avoid off-by-one errors when applying substring and length operations.
    Common Mistakes
    • Assumes indexing always starts at 1 and gives an off-by-one answer; check the convention of the language being used.
    • Treats the position as the character itself rather than its index; the function returns a number, not the matched text.
    • Ignores the not-found case and uses the returned sentinel value as if it were a valid index.
    • Assuming slicing parameters work identically across all platforms: Python uses [start:stop] where stop is non-inclusive, whereas AQA pseudo-code and Visual Basic often use (start, length).
    • Believing string concatenation directly modifies existing memory: in many high-level languages, strings are immutable, meaning concatenation creates a newly allocated string rather than appending in-place.
    • Treating numeric characters as integers without explicit casting: the character '7' has an ASCII value of 55 and cannot be used directly in arithmetic operations without conversion.
    Revision Plan
    1. 1Day 1-2: Review core pseudo-code string primitives (LEN, SUBSTRING, POSITION, CHAR_TO_CODE, CODE_TO_CHAR) and map them to your Paper 1 language methods.
    2. 2Day 3-4: Practise trace tables featuring nested loops and substring extraction to eliminate off-by-one index errors.
    3. 3Day 5-6: Solve string validation and parsing past paper problems (e.g. validating postcodes, IP addresses, or credit card check digits).
    4. 4Day 7: Complete timed Paper 1 modifications involving string manipulation within the current year's Skeleton Program context.
    Exam Question Types
    • 📋Pseudo-code trace tables: tracing the contents of variables as a loop manipulates and slices a text string.
    • 📋Algorithmic design (Paper 2): writing pseudo-code algorithms to parse, encrypt, validate, or format raw input strings.
    • 📋On-screen code adaptation (Paper 1): adding validation routines or string tokenisation to the pre-released Skeleton Program.
    Command Word Expectations (AQA)
    Write an algorithm

    Construct fully structured, syntactically coherent pseudo-code or program code that solves the stated problem, showing correct parameter usage and variable assignments.

    Trace

    Follow code execution step by step, recording every change to variables, index pointers, and string states into a structured trace table.

    How Students Lose Marks (Examiner Pitfalls)
    Pitfall: Confusing 0-based indexing with 1-based indexing when calculating substring offsets in pseudo-code versus actual programming languages like Python or C#.
    ❌ Weak Answer (Loses Marks):The substring starts at index 3 so the characters extracted are from position 3 to 7 without clarifying whether index 3 is the third or fourth character.
    Example improved answer:In AQA pseudo-code, string indices are 0-indexed unless explicitly stated otherwise. Executing SUBSTRING(3, 4, 'COMPUTING') starts at index 3 (the 4th character, 'P') and extracts 4 characters, resulting in 'PUTI'.
    Examiner Tip: Always explicitly state index conventions or follow the exact indexing defined in the question paper insert. In Paper 1, verify whether your chosen language slicing includes or excludes the stop index.
    Pitfall: Failing to account for string immutability when updating individual characters within a string during algorithmic questions.
    ❌ Weak Answer (Loses Marks):str[2] = 'X' to change the character in the string directly.
    Example improved answer:Because strings are immutable in languages such as Python and C#, direct assignment by index causes a runtime error. The string must be reconstructed using concatenation or slicing: str = SUBSTRING(0, 2, str) + 'X' + SUBSTRING(3, LEN(str) - 3, str).
    Examiner Tip: When answering theoretical Paper 2 questions, remember that primitive string manipulation involves creating a new string instance rather than in-place mutation.
    Step-by-Step Worked Solutions

    Question: A student needs to validate a UK driving licence format in pseudo-code. A valid code starts with 5 uppercase letters, followed by 6 digits, and ends with 2 letters. Write a function IsFormatValid(licence) that returns True if the licence matches the length of 13 characters and contains digits in characters at indices 5 to 10 (0-indexed), returning False otherwise.

    1. 1.Step 1: Check the overall length of the input string using LEN(licence). If LEN(licence) != 13, immediately return False.
    2. 2.Step 2: Iterate through the indices 5 to 10 inclusive to verify numeric characters.
    3. 3.Step 3: For each index i from 5 to 10, extract the single character using SUBSTRING(i, 1, licence) and check if its ASCII character code is between 48 ('0') and 57 ('9'), or use an isDigit check.
    4. 4.Step 4: If any character fails the check, return False. If the loop completes and length is valid, return True.
    Final Answer: FUNCTION IsFormatValid(licence : STRING) RETURNS BOOLEAN IF LEN(licence) != 13 THEN RETURN False ENDIF FOR i <- 5 TO 10 ch <- SUBSTRING(i, 1, licence) IF ch < '0' OR ch > '9' THEN RETURN False ENDIF ENDFOR RETURN True ENDFUNCTION

    Question: Given the string text = 'EXAMINATION', determine the exact output of: Result <- SUBSTRING(4, 4, text) followed by NewResult <- Result + CHAR_TO_CODE(SUBSTRING(0, 1, text)). Assume 0-indexed strings where SUBSTRING parameters are (startIndex, length, sourceString), and ASCII encoding applies.

    1. 1.Step 1: Identify indices of 'EXAMINATION' (0: 'E', 1: 'X', 2: 'A', 3: 'M', 4: 'I', 5: 'N', 6: 'A', 7: 'T', 8: 'I', 9: 'O', 10: 'N').
    2. 2.Step 2: SUBSTRING(4, 4, text) starts at index 4 ('I') and takes 4 characters ('I', 'N', 'A', 'T'), yielding Result = 'INAT'.
    3. 3.Step 3: SUBSTRING(0, 1, text) extracts the character at index 0, which is 'E'.
    4. 4.Step 4: CHAR_TO_CODE('E') evaluates to ASCII code 69 (since 'A' is 65, 'B' is 66, 'C' is 67, 'D' is 68, 'E' is 69).
    5. 5.Step 5: Concatenating string 'INAT' with string representation of 69 gives 'INAT69'.
    Final Answer: 'INAT69'
    Active Recall Memory Test
    What does the function CHAR_TO_CODE('A') return in standard ASCII?
    Key Fact: 65
    How does 0-based indexing differ from 1-based indexing when accessing the third character of 'PYTHON'?
    Key Fact: In 0-based indexing it is accessed at index 2 ('T'); in 1-based indexing it is accessed at index 3 ('T').
    What is the result of concatenating '12' and '34' versus adding integers 12 and 34?
    Key Fact: Concatenation produces the string '1234', whereas integer addition produces the numeric value 46.
    Frequently Asked Questions
    What is the difference between string length and the last index of a string?
    In 0-indexed systems, the last character index is always equal to the total string length minus 1. For instance, the word 'CAT' has a length of 3, but its characters occupy indices 0, 1, and 2. Attempting to access the character at the index equal to the string length results in an 'index out of bounds' runtime error.
    How does AQA pseudo-code define the SUBSTRING function?
    AQA pseudo-code typically defines SUBSTRING with parameters specifying the start position, the number of characters to extract, and the source string, such as SUBSTRING(start, length, string). However, you must always verify the exact specification sheet or exam paper preamble, as some specifications specify (string, start, length). In your Paper 1 language, remember that slicing syntax (like Python's text[start:end]) specifies an end boundary rather than a character count.
    Why do strings need to be converted to ASCII or Unicode values in algorithms?
    Converting characters to their numerical ASCII or Unicode values allows programs to evaluate alphabetical order, perform Caesar cipher transformations, and validate character ranges algebraically (for instance, verifying whether a character code falls between 65 and 90 to confirm it is an uppercase letter). This conversion is executed using functions like ORD() or CHAR_TO_CODE().
    What happens if I try to change a single character in a string in Python during Paper 1?
    In Python, strings are immutable objects, meaning you cannot assign a new character directly to an indexed position (e.g. text[0] = 'A' raises a TypeError). To modify a string, you must create a new string by concatenating substrings or convert the string into a mutable list of characters, modify the element, and rejoin it.