Chhetri AcademyGCSE & A level Paper Builder

String manipulation

OCR GCSE Computer Science (J277) · Algorithms and programming (Paper 2) › Programming fundamentals › Additional programming techniques

Practise String manipulation. 11 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on String manipulation (Craig 'n' Dave OCR on YouTube) · Practise all of Additional programming techniques

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

A program contains the line word = "Keyboard". In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0. State the value of word.length.
8
Look at this program.
01 first = "Ada"
02 last = "Lovelace"
03 full = first + " " + last
04 print(full)
05 print(full.length)
State the output of line 04.
Ada Lovelace

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
A program contains the line word = "Keyboard". In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) State the value of word.length.[1]
(b) State the value of word.upper.[1]
(c) State the value of word.substring(0, 3).[1]
(d) State the value of word.substring(3, 5).[1]
Show the answer and mark scheme
(a) Answer: 8
  • 8
(b) Answer: KEYBOARD
  • KEYBOARD (all capitals)
(c) Answer: Key
  • Key
(d) Answer: board
  • board
Question 2Medium7 marks
A school creates each pupil's username from:
  • the first three letters of their surname
  • the first letter of their first name
  • the last two digits of the year they joined.
The username is in upper case. For example, Mia Patel, who joined in 2025, has the username PATM25. In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) Write an algorithm that asks the user to enter a pupil's first name, surname and year of joining (as a 4-character string), and outputs the pupil's username.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[5]
(b) State the username for Jo Lin, who joined in 2024.[1]
(c) Explain a problem that could happen if a pupil's surname has only two letters, for example Ng.[1]
Show the answer and mark scheme
(a) Answer:
first = input("First name")
surname = input("Surname")
year = input("Year joined")
user = surname.substring(0, 3) + first.substring(0, 1)
user = user + year.substring(2, 2)
print(user.upper)
  • inputs the first name, surname and year
  • extracts the first three letters of the surname (substring(0, 3))
  • extracts the first letter of the first name (substring(0, 1))
  • extracts the last two digits of the year (substring(2, 2))
  • joins the parts in the correct order, converts to upper case and outputs the username
(b) Answer: LINJ24
  • LINJ24
(c)
  • substring(0, 3) asks for more characters than the surname has, so an error could occur / the username would be shorter than expected
Question 3Hard7 marks
A password is valid if it:
  • is at least 8 characters long
  • contains at least one digit (0 to 9)
  • contains at least one upper-case letter (A to Z).
The ASCII codes for the digits 0 to 9 are 48 to 57, and for the letters A to Z are 65 to 90. In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) Write a function that takes a password as a parameter and returns True if the password is valid, or False if it is not.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[6]
(b) Give an example of a password that is at least 8 characters long but is not valid, and state why it is not valid.[1]
Show the answer and mark scheme
(a) Answer:
function isValid(pw)
   hasDigit = false
   hasUpper = false
   for i = 0 to pw.length - 1
      code = ASC(pw.substring(i, 1))
      if code >= 48 AND code <= 57 then
         hasDigit = true
      elseif code >= 65 AND code <= 90 then
         hasUpper = true
      endif
   next i
   return pw.length >= 8 AND hasDigit AND hasUpper
endfunction
  • checks that the length is at least 8
  • loops through every character of the password
  • uses ASC to find the code of each character
  • records (flag / counter) when a code is from 48 to 57
  • records (flag / counter) when a code is from 65 to 90
  • returns True only when all three conditions are met, otherwise False
(b)
  • a suitable example with its reason, e.g. "sunshine7" has no upper-case letter / "Sunshines" has no digit

Related subtopics

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial