levo

Fundamentals of Programming

String-handling operations

  • 3 things to know
  • Paper 1

Overview

Recall questions on length, position, substring and concatenation, on where positions are counted from, and on converting between characters and their character codes.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

What does the length operation do? What is the length of 'Computing' and of the empty string?

Process

What does a string to float conversion do, and when do you need it rather than string to integer?

Key term

What does a date/time to string conversion do?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does the length operation do? What is the length of 'Computing' and of the empty string?

    Model answer

    It returns the number of characters in a string. 'Computing' has length 9 and the empty string has length 0. Spaces and punctuation are characters and count towards the length, so 'A B' has length 3.

  2. 2

    What does the position operation do, and what does it return when the item is not there?

    Model answer

    It searches a string for a character or a substring and returns the index at which the first occurrence starts. If the item does not occur, it returns an agreed value that cannot be a valid index, typically -1 or 0 depending on the language. Only the first occurrence is reported, so finding later ones means searching again from beyond that point.

  3. 3

    Where does the position of a character get counted from, and why must you check this?

    Model answer

    Some languages number the characters of a string from 0 and others from 1, so in 'Computing' the letter 'm' is at position 2 counting from zero but at position 3 counting from one. Getting the base wrong produces answers that are consistently out by one, which is one of the commonest string-handling bugs.

  4. 4

    What does the substring operation do? Extract the first three characters of 'Computing'.

    Model answer

    It returns a section of a string, given a starting position and either a length or an end position. Taking three characters from the start of 'Computing' gives 'Com'. The original string is unchanged; the operation returns a new string.

  5. 5

    What does concatenation do, and what does 'Comp' joined to 'uting' give?

    Model answer

    It joins two strings end to end to produce a single longer string, so 'Comp' and 'uting' concatenate to 'Computing'. Nothing is inserted between them, so joining a first name to a surname needs a space concatenated in as well. In most languages the operator is +, though some use & or a join function.

  6. 6

    What does the character to character code operation do? What is the code for 'A'?

    Model answer

    It returns the numeric code that represents a given character in the coding scheme being used, so in ASCII the character 'A' gives 65. It is often written ord(c) or ASC(c).

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Programming, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.