±«Óătv

String handling operations

A is a data type used to represent a sequence of one or more alphanumeric characters. These characters can be letters, numbers or symbols. It is usually possible to manipulate a string to provide information or to alter the contents of a string. Strings are shown in quotes (single or double), for example: 'Hello', or "World!".

A can have string as its data type. For example:

wordOne = "Computer"
wordTwo = "Science"

Length

The length of a string can be determined using the LEN. This gives the length as an .

LEN(wordOne) - would give the answer "eight", as there are eight characters in the word "Computer"

Character position

It is possible to determine which character features at a position within a string:

wordOne[2] - would give the answer "m", as m is the third character in the word “Computer” (remember computers start counting at zero).

Substring

A substring is a string of characters that exists inside a string, for example, the first three characters of a password.

The code below extracts a substring of the first three letters of a string in two ways:

Code version one:

wordOne ← “Computer”
sub ← “”
FOR count ← 0 TO 2
     sub ← sub + wordOne[count]
ENDFOR
OUTPUT sub

Code version two:

wordOne ← “Computer”
OUTPUT SUBSTRING(0, 2, wordOne)

Both versions would give “Com”, the first three characters in the string. However, using the SUBSTRING function saves having to write as much code each time a substring is needed.

The SUBSTRING function takes three parameters:

  • an integer that indicates the starting point in the string
  • an integer that indicates the end point within the string
  • the string that the substring is to be taken from

Concatenation

is the joining together of two strings . In the following program, the variable 'fullName', is created by joining the 'firstName' and 'lastName' values together with a space between the strings.

firstName ← “Bob”
lastName ← “Smith”
fullName ← firstName + " " + lastName
OUTPUT fullName

This would output the name Bob Smith.

String conversion

Sometimes a programmer needs to change the stored within a variable. For example, an integer may need to be converted to a string in order to be displayed as part of a message. This process is known as . The following examples convert a string to an integer and an integer to a string using Python:

str(68) returns “68”
int(“54”) returns 54

AQA pseudo-code uses the following functions:

  • STRING_TO_INT - string
  • STRING_TO_REAL - string
  • INT_TO_STRING - integer
  • REAL_TO_STRING - real

Character set conversions

Another way of dealing with is to convert alphanumeric data into its numerical format in a . The pseudo-code CHAR_TO_CODE and CODE_TO_CHAR functions allow characters to be swapped between their text and number formats using the or character set. Read more about these character sets in the study guide.

CHAR_TO_CODE(character) - converts from alphanumeric character to ASCII/UNICODE character

CODE_TO_CHAR(integer) - converts from ASCII/UNICODE character to alphanumeric character

In the code below, the user enters a character. The code checks that this is between lower-case ‘a’ and ‘z’.

letter ← USERINPUT
IF (CHAR_TO_CODE(character) >= CHAR_TO_CODE(“a”))
AND
(CHAR_TO_CODE(character) <= CHAR_TO_CODE(“z”))
THEN
    OUTPUT “Character correct!”
ELSE
OUTPUT “Incorrect”
ENDIF

In this example, CHAR_TO_CODE has been used to create a range check for the data.

In contrast, CODE_TO_CHAR takes an ASCII character and converts it to the alphanumeric character. This is useful when random letters need to be generated. For example, in the code below a random number is converted to a letter as part of a word guessing game.

letter ← RANDOM_INT(97,122)
OUTPUT “does it contain” + CODE_TO_CHAR(character) + “?”