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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- The use of variables and constants, the common data types (integer, real, Boolean, character and string), choosing an appropriate data type, and casting (converting) between data types.
An OCR J277 2.2.1 answer on variables and constants, the common data types (integer, real, Boolean, character, string), choosing an appropriate data type for data, and casting between data types.
- Using one-dimensional and two-dimensional arrays, the use of records to store structured data, and basic SQL (SELECT, FROM, WHERE) to search records in a database.
An OCR J277 2.2.3 answer on storing structured data: one-dimensional and two-dimensional arrays, records, and using basic SQL (SELECT, FROM, WHERE) to search records in a database table.
- The use of subprograms (procedures and functions), passing parameters into a subprogram, returning values from a function, local versus global variable scope, and generating random numbers.
An OCR J277 2.2.3 answer on subprograms: procedures and functions, passing parameters, returning values, the difference between local and global variables, the benefits of subprograms, and generating random numbers.
- The three basic programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), and when to use each.
An OCR J277 2.2.2 answer on the three programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), with the OCR Exam Reference Language for each.
- Representing characters with ASCII and Unicode; representing images with pixels, colour depth, resolution and metadata; representing sound with sample rate, sample resolution and bit rate; and the effect on file size and quality.
An OCR J277 1.2.4 answer on representing characters (ASCII, Unicode), images (pixels, colour depth, resolution, metadata) and sound (sample rate, sample resolution, bit rate), and the effect of each on file size and quality.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)