Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you manipulate strings and read from and write to text files?

String manipulation (length, position, substring, concatenation and changing case, and converting between characters and character codes) and basic file handling (opening, reading, writing and closing text files).

An OCR J277 2.2.3 answer on string manipulation (length, substring, concatenation, case change, character codes with ASC and CHR) and basic file handling (opening, reading, writing and closing text files) in the OCR Exam Reference Language.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. String manipulation
  3. Character codes
  4. File handling
  5. Try this

What this dot point is asking

OCR wants you to manipulate strings (find their length, take part of them, join them, change case, and convert between characters and their codes) and to do basic file handling (open, read, write and close text files). Both are "additional programming techniques" tested in Paper 2, including Section B, where reading data from a file and processing strings are common.

String manipulation

These combine to validate and reformat text. To get a username's initial: initial = name.substring(0,1).upper. To build a message: print("Welcome, " + name + "!").

Character codes

File handling

Try this

Q1. State the result of "Computer".length and "Computer".substring(0,4). [2 marks]

  • Cue. Length is 8; substring(0,4) is "Comp".

Q2. Write a line that joins firstName and surname into fullName with a space between. [1 mark]

  • Cue. fullName = firstName + " " + surname.

Q3. State one reason a program should close a file after using it. [1 mark]

  • Cue. So the file is not left locked for other programs, and so data being written is saved properly.

Exam-style practice questions

Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

OCR 20214 marksA variable name holds the string "Patel". State the result of each: (a) name.length, (b) name.upper, (c) name.substring(0,3). Then write a line that joins the strings in firstName and surname with a space between them into a variable fullName.
Show worked answer →

(a) name.length is 5 (the number of characters in "Patel").

(b) name.upper is "PATEL" (every character in upper case).

(c) name.substring(0,3) is "Pat": starting at index 0, taking 3 characters (P, a, t). Arrays and strings are 0-indexed in the OCR Exam Reference Language.

Joining (1 mark): fullName = firstName + " " + surname. Concatenation with + joins the strings, and the " " adds the space.

Markers reward the three correct results and the correct concatenation. A common error on substring is miscounting the start index or the length.

OCR 20225 marksWrite an algorithm that opens a text file called scores.txt, reads each line until the end of the file, and prints each line. Describe what would happen if the file was not closed afterwards.
Show worked answer →

Algorithm (up to 4): open the file for reading, loop until the end of file reading and printing each line, then close the file.

myFile = openRead("scores.txt")
while NOT myFile.endOfFile()
  line = myFile.readLine()
  print(line)
endwhile
myFile.close()

Marks: open for reading (1), loop using endOfFile (1), read and print each line (1), close the file (1).

Not closing the file (1): the file may stay locked so other programs cannot use it, data being written may not be saved properly, and system resources are wasted. Markers reward a correct read loop with endOfFile, the close, and a sensible consequence of leaving it open.

Related dot points

Sources & how we know this