DoRevision

String Surgeon

Five things you can do to a piece of text, and the fact that catches everybody: not one of them changes the text you started with. What each operation takes, and what it hands back.

⏱️ 16 min 🎯 14 activities
Best used for
Homework Independent study Mock preparation

Get the method right under pressure

Free interactive practice on the steps that lose marks under exam pressure.

Start revising free

What you'll cover

Nothing you do changes the string

You already know the difference between a character and a string: one symbol against a sequence of them. This topic is about what you can DO to a sequence. ⚠️ And there is one fact underneath all of it that catches almost everybody. Not one of these operations changes the string you started with. That sounds like a technicality. It is the source of the commonest bug on this topic: somebody converts a name to capitals, prints the variable, and finds it still in lower case. Nothing went wrong. The operation handed back a new string and nobody caught it. So the useful question about any of these is never really "what does it do to the string". It is what does it hand back. Some hand back a fact about the text. Some hand back a new piece of text. Knowing which is which is most of this topic.

The operations this paper names

Five operations. Read each one for what it gives back, not just for what it is called.

What you get back

Sort them by what lands in your hands afterwards, and the third column is true of every single one of them.

How to answer a string question

Two things, in this order, and you will very rarely be wrong. What goes IN, and what comes BACK. Naming the operation is worth almost nothing on its own, because the name is usually in the question already. Saying what it takes and what it returns is the part that shows you know what it is for. ⚠️ Then add one clause: is what came back a fact about the text, or a piece of text in its own right? That decides what you are allowed to do with it next, and it is the distinction a follow-up question will test. ⚠️ And when you are counting positions, be careful at the far end rather than the near one. Starting the count where it starts is easy to remember. Working out where the count therefore FINISHES is where the mistakes actually happen, and a question that asks about the final character is asking about exactly that.

Match each operation to what it hands back

  • Finding the length
  • Taking a substring
  • Concatenation
  • Converting a character to its code
  • a number saying how many characters the text contains
  • a shorter piece of text, taken from inside the original
  • a longer piece of text, made by joining two together
  • a number standing for one single character

The variable that did not change

A program stores the text bristol in a variable, converts it to upper case, then prints that same variable. It prints bristol, in lower case. Why?

  • The conversion handed back a new string. The original variable was never altered, and the result was not stored anywhere
  • The conversion failed because the string was already in lower case
  • Case conversion only works on one character at a time
  • The variable was declared as a character rather than as a string

One word, five operations

Take the string Bristol and put every operation through it, so you can see them against the same piece of text. Its length is 7, because it holds seven characters. Positions are counted from zero, so the character at position 0 is B. ⚠️ And here is the consequence worth more than the rule: the LAST character sits at position 6, which is one less than the length. That is why off-by-one mistakes happen at the far end of a string and almost never at the near end. A substring here is asked for as a starting position and a count. Starting at position 0 and taking 3 characters gives Bri. Concatenation joins two strings end to end: Bris with tol gives Bristol. Case conversion gives a copy in capitals: BRISTOL. ⚠️ And Bristol is still exactly Bristol afterwards, which is the fact the whole module rests on. Character-code conversion turns a single character into the number standing for it, and back again. In ASCII the code for A is 65. The code tables themselves belong to the data representation topic, which other modules cover; here it is enough to know the conversion runs both ways.

The position of the last character

A variable holds the text Bristol, which is 7 characters long. Positions in the string are counted starting from zero. Work out the position number of the very last character.

Complete the string operations paragraph

Joining two pieces of text end to end is called _____. Pulling a shorter run of characters out of a longer piece of text gives you a _____. The count of how many characters a piece of text holds is its _____. Swapping between a single character and the numeric code that represents it is _____ conversion.

concatenation substring length character-code upper case lower case validation casting

Which operation

Five jobs, three lives. Pick the operation that does what is being asked for.

Build the display name

A program has a first name and a surname stored separately, and needs one tidy display name. Put the stages in order.

  • Start with the first name and the surname held in two separate variables
  • Convert both to a consistent case, so they always display the same way
  • Join the first name to a single space
  • Join that result to the surname
  • Store the finished string, since neither original variable has changed

Spot the true string facts

Tap the TWO statements that are true.

  • Converting a string to upper case hands back a new string and leaves the original unchanged
  • When positions start at zero, the last character sits one below the length
  • Concatenation removes the two original strings and replaces them with the joined one
  • Finding the length of a string hands back a shorter string

Three text jobs

Three situations. Choose the answer that says what comes back, not just what it is called.

  • A program needs the first two letters of a postcode in order to work out a region. Which operation, and what does it give back?
  • Two users type the same name with different capitals, and the program reports that they do not match. What is the fix?
  • A student writes in an answer that an operation "changed the string". What should they have written instead?

Explain what each operation gives back

A friend keeps writing that string operations "change the text", and cannot work out why their program prints the wrong thing. Write them the answer.

  • Name the string operations this specification lists
  • For each one, say what goes in and what comes back
  • Explain what is true of the original string after any of them has run
  • Explain why the last character sits at a position one below the length
  • Finish with the bug this misunderstanding causes, and what to do so it does not happen