Learning Objectives
  • Use LENGTH, SUBSTRING, UCASE and LCASE.
  • Explain string positions and substring parameters.
  • Normalise text before comparison.
  • Apply string operations to validation and processing.
Key Terms
String operation
A routine that examines or produces string or character data.
LENGTH
Returns the number of characters in a string.
SUBSTRING
Returns a selected sequence of characters from a string.
UCASE
Returns text converted to upper case.
LCASE
Returns text converted to lower case.
Start position
The position at which a substring begins.
Summary diagram
Summary Of The Main Ideas In This Lesson
LENGTH

LENGTH(StringValue) returns an INTEGER equal to the number of characters. Spaces and punctuation are characters and contribute to the length.

LENGTH is useful for length validation, locating the last character and controlling a loop over the characters of a string.

For the official example, LENGTH(“Happy Days”) returns 10 because the space is counted.

UCASE And LCASE

UCASE returns the string or character in upper case; LCASE returns it in lower case. These routines do not change the original variable unless their result is assigned back.

Case conversion is useful before comparison. Converting both a stored command and input to upper case makes q and Q equivalent.

Non-letter characters remain unchanged. The routines may accept STRING or CHAR values.

SUBSTRING

SUBSTRING(identifier, start, length) returns a string containing the requested number of characters beginning at the specified position. Cambridge generally uses position 1 for the first character, although questions may state that indexing begins at 0 or 1.

The second argument is the starting position and the third is the number of characters, not the ending position. SUBSTRING(“COMPUTER”, 2, 3) returns “OMP” when the first position is 1.

The start and length should be positive integers and the requested portion should lie within the string.

Combining String Operations

The inner SUBSTRING is evaluated first, then UCASE converts its one-character result. Nested routine calls can be useful but should remain readable.

INPUT FullName
NameLength <- LENGTH(FullName)
FirstLetter <- UCASE(SUBSTRING(FullName, 1, 1))
OUTPUT "Characters: ", NameLength
OUTPUT "First letter: ", FirstLetter
String Validation

A program can combine string routines with selection. For example, a code may require exactly eight characters and begin with A. Test LENGTH(Code) = 8 AND UCASE(SUBSTRING(Code, 1, 1)) = “A”.

String operations do not by themselves prove that every character follows a complex format. Only use checks that can be expressed using the syllabus routines and conditions provided by the question.

Routine Summary
Routine Return Type Example Result
LENGTH(“CAT”) INTEGER 3
UCASE(“Cat”) STRING CAT
LCASE(“A”) CHAR or string value a
SUBSTRING(“COMPUTER”, 1, 4) STRING COMP
Worked Examples
Initials

Question: Extract the first character of FirstName and convert it to upper case.

  1. Use SUBSTRING with start 1 and length 1.
  2. Apply UCASE to the returned character or string.

Answer: Initial <- UCASE(SUBSTRING(FirstName, 1, 1)).

Minimum Length

Question: Test whether Password contains at least eight characters.

  1. Use LENGTH to obtain the number of characters.
  2. Compare the result with 8 using >=.

Answer: LENGTH(Password) >= 8.

Examination Guidance
  • State whether the first position is 0 or 1 if the question specifies it.
  • Remember the third SUBSTRING argument is length.
  • Assign the result of UCASE or LCASE if the variable itself must change.
  • Count spaces in string length.
  • Use case conversion before case-insensitive comparisons.
Common Mistakes
  • Treating SUBSTRINGs third argument as an ending position.
  • Assuming UCASE automatically changes the original variable.
  • Ignoring spaces in LENGTH.
  • Using arithmetic on numeric characters without conversion.
  • Starting at the wrong position.
Knowledge Check

1. What does LENGTH return?

Answer: The number of characters as an INTEGER.

2. What does UCASE do?

Answer: Returns the text converted to upper case.

3. What are the SUBSTRING parameters?

Answer: The string, start position and number of characters.

4. Does UCASE always change the original variable?

Answer: No; only if its result is assigned back.

5. What does SUBSTRING(“HOUSE”, 2, 2) return with position 1 first?

Answer: OU.