That Blue Square Thing

AQA Computer Science GCSE

July 2019: the AQA CompSci area is almost complete. Most of the content for units 3-6 is now up and there's quite a lot of Unit 7 material. There's still some work to do, particularly on Paper 1 units, but it's getting there.

A String Problem

This is based on an exam question. The point is to show the sorts of things you might have to do in an exam to do with strings.

The question is a Paper 1 style question - so it involves problem solving using algorithms and so on rather than being based on knowledge.

Strings and Arrays

The problem demonstrates the similarities between strings and arrays.

Strings are essentially arrays of characters. Just like arrays they have a length and it is possible to iterate over a string looking at every character in it. So...

stringA <- "bitmap"

stringB <- ['b', 'i', 't', 'm', 'a', 'p']
IF LEN(stringA) = LEN(stringB) THEN
OUTPUT "Same length"
ELSE
OUTPUT "Different length"
ENDIF

IF stringA[0] = stringB[0] THEN
OUTPUT "Same first letter"
ELSE
OUTPUT "Different first letter"
ENDIF

In the example both selection statements should be True - the two variables have the same length and the same first element (the character 'b').

Try to code this and see if it works. The Python shouldn't be very much different.

Strings and Substrings

But strings are also different to arrays. They can be concatenated more easily and they have substrings which allow strings to be broken up.

Here's a problem to solve:

Develop a subroutine called firstThree. The subroutine should check whether the first three characters of a string entered by the user are the same as the first three characters of a string stored in a variable called theString. A suitable message should be displayed.

So, in the pseudocode below, if the user entered the word "bitumen" then the first three letters of the word stored in theString ("bitmap") are the same. If they entered the word "biscuit" then the first three letters are different.

theString <- "bitmap" # theString might have another value
userString <- USERINPUT

# compare the first three letters
# and display a suitable message
# hint: use substring

Your code should work for any values of theString with at least three characters.

You could enhance your solution by:

There is an exam question version of this below. This is a Paper 1 exam style question. In an exam you would need to use pseudocode or a flowchart to answer the question. Remember that you don't need to write perfect pseudocode.

PDF iconA String Problem - PDF exam style question.

A Harder String-Array Problem

Try this Paper 1 style question - that I think is a bit harder. It looks at arrays rather than strings. Working out what to look at takes some really careful thought.

PDF iconAnother String-Array Problem - PDF exam style question.