CIE IGCSE | 8.1.4E.2 Uppercase, Lowercase and String Positions

Lesson objective

Use UCASE and LCASE, preserve original text when needed, compare normalised values and inspect characters at one-based string positions.

Learn

8.1.4E.2 | UPPERCASE, LOWERCASE AND STRING POSITIONS

01 | THE SAME CHOICE CAN BE TYPED IN DIFFERENT CASES

A menu asks someone to enter Y for yes. One user enters Y and another enters y. If the task intends both to mean the same choice, the program needs to handle that rule explicitly.

Uppercase means capital-letter form, such as A or B. Lowercase means small-letter form, such as a or b. Letter case describes the form of the letter, not its position in a string.

Cambridge pseudocode uses UCASE to produce an uppercase result and LCASE to produce a lowercase result. Both work with STRING or CHAR values.

We will use ordinary Latin letters, digits, punctuation and spaces for these examples. First understand conversion, then combine it with comparisons and numbered character positions.

02 | UCASE RETURNS AN UPPERCASE RESULT

UCASE converts the letters in its argument to uppercase. An argument is the value supplied to an operation. The operation returns a result that another instruction can use.

DECLARE OriginalText : STRING
DECLARE UpperText : STRING
OriginalText ← "Cs Cafe-07"
UpperText ← UCASE(OriginalText)
OUTPUT UpperText

UpperText becomes "CS CAFE-07". The letters change to capital form; the space, hyphen and digits stay as they were.

The source OriginalText remains "Cs Cafe-07" because the result is assigned to a different variable. UCASE does not automatically replace every variable holding that text.

Use the Cambridge name UCASE rather than importing another language’s upper or uppercase method notation.

03 | LCASE RETURNS A LOWERCASE RESULT

LCASE performs the corresponding lowercase conversion.

DECLARE OriginalText : STRING
DECLARE LowerText : STRING
OriginalText ← "Cs Cafe-07"
LowerText ← LCASE(OriginalText)
OUTPUT LowerText

The result is "cs cafe-07". Already-lowercase letters stay lowercase, while uppercase letters become lowercase. The non-letter characters in this example stay unchanged.

An empty string remains empty
SourceUCASE resultLCASE result
"Cs Cafe-07""CS CAFE-07""cs cafe-07"
"Banana""BANANA""banana"
"0072""0072""0072"
""""""

Neither operation makes digits into letters, removes spaces or converts a numeric-looking string to an INTEGER. Those are different operations or requirements.

04 | CHAR CONVERSION STAYS A CHARACTER

The operations also accept CHAR. A one-character CHAR result stays CHAR rather than becoming a multi-character string.

DECLARE Direction : CHAR
Direction ← 'n'
Direction ← UCASE(Direction)
OUTPUT Direction

Direction becomes the character N. Here, the assignment uses the same destination, so the original lowercase value is replaced.

For a STRING source, the operation returns STRING. For a CHAR source, it returns CHAR. Choose a receiving variable with the appropriate type.

This differs from SUBSTRING, whose result is STRING even when it extracts only one character. The displayed letter may look identical while the type is different.

05 | CONVERSION AND ASSIGNMENT HAVE DIFFERENT JOBS

Read these alternatives with Text initially holding "Maya".

Each alternative starts separately from Text equal to Maya
InstructionOutput or assignment resultText afterwards
OUTPUT UCASE(Text)Displays MAYAMaya
UpperText ← UCASE(Text)Stores MAYA in UpperTextMaya
Text ← UCASE(Text)Stores MAYA in TextMAYA

The operation produces a converted result. The assignment destination determines which stored item changes. OUTPUT simply communicates the result.

Preserve original text if its presentation matters. A name may be useful in its entered form for display, even if a separate converted copy is needed for comparison.

Do not assume a later source change automatically recalculates a converted copy. Re-execute the conversion assignment when a refreshed result is required.

06 | CASE-INSENSITIVE COMPARISON NEEDS A COMMON FORM

A case-sensitive comparison distinguishes letter case. A case-insensitive comparison treats corresponding uppercase and lowercase letters alike.

Normalising here means converting both values to a common letter-case form before comparing them.

DECLARE Answer : STRING
OUTPUT "Enter Y for yes"
INPUT Answer
IF UCASE(Answer) = "Y"
  THEN
    OUTPUT "Yes selected"
  ELSE
    OUTPUT "Not a yes response"
ENDIF

The literal Y is already uppercase, so converting Answer to uppercase makes y and Y agree with it. A response of yes does not match the single-character string Y.

If both sides come from variables, convert both consistently, such as UCASE(FirstText) = UCASE(SecondText). Converting just one side can leave a mismatch.

Only normalise when the requirement permits it. Some data is intended to distinguish case, and silently changing that rule would be incorrect.

07 | CASE CONVERSION IS NOT FULL INPUT CLEANING

The input " y " contains a space, y and another space. UCASE produces " Y ", which is still three characters and does not equal the one-character string "Y".

Converting case does not remove surrounding spaces, reject punctuation or establish that a code exists. A length or format requirement still needs its own handling.

The syllabus treats identifier names as case-insensitive, but that naming rule does not mean every stored text value automatically ignores case. A variable name and its data are separate things.

Explain the actual comparison rule rather than saying “computers do not care about capitals”. The algorithm decides how the input should be interpreted.

08 | REVISIT ONE-BASED CHARACTER POSITIONS

A character position identifies its numbered place in a string. In these Cambridge examples, the first position is one.

1C
2s
3SPACE
4C
5a
6f
7e
8-
90
107

For Cs Cafe-07, the length is ten. Position three is a space, eight is a hyphen and nine is the digit zero.

For the ordinary letters in this example, case conversion changes their form without moving their positions. After UCASE, position two becomes S and position seven becomes E.

The SPACE label in the illustration represents one stored blank character. It is a visual aid, not text inserted into the source.

09 | INSPECT ONE POSITION WITH SUBSTRING

Use SUBSTRING with an extraction length of one to retrieve the string at a particular position.

DECLARE Text : STRING
DECLARE Part : STRING
Text ← "Cs Cafe-07"
Part ← SUBSTRING(Text, 4, 1)
OUTPUT Part

The result is the string "C" because position four is C. Part is STRING, consistent with the operation’s return type.

To inspect a position safely, require Position at least one and no greater than LENGTH(Text). If the string is empty, no character position is available.

IF Position >= 1 AND Position <= LENGTH(Text)
  THEN
    Part ← SUBSTRING(Text, Position, 1)
    OUTPUT "Character at position: ", Part
ENDIF

Assume suitable declarations and an INTEGER Position. Do not use an invented POSITION or FIND routine when the question expects the string operations specified in this syllabus.

10 | EXPLORE CASE AND POSITIONS TOGETHER

Enter basic letters, digits, punctuation and spaces. Select a position to inspect and a single-character target to count without distinguishing letter case.

Try Banana with target A. The matching positions are two, four and six. The count is three even though the original text uses lowercase a.

11 | SCAN THE STRING ONE POSITION AT A TIME

To count occurrences, visit each position and compare its one-character substring with the target. A loop body is the instruction or group of instructions repeated on each pass.

DECLARE Text : STRING
DECLARE NormalText : STRING
DECLARE Position : INTEGER
DECLARE MatchCount : INTEGER
Text ← "Banana"
NormalText ← UCASE(Text)
MatchCount ← 0
FOR Position ← 1 TO LENGTH(NormalText)
    IF SUBSTRING(NormalText, Position, 1) = "A"
      THEN
        MatchCount ← MatchCount + 1
    ENDIF
NEXT Position
OUTPUT "A or a occurrences: ", MatchCount

NormalText is BANANA. The loop visits positions one through six and increments MatchCount at two, four and six. Its final result is three.

The total string length is not the occurrence count. Six positions are examined, but only three meet the inclusion condition.

For an empty source, LENGTH is zero and the default FOR from one to zero has no body executions. The initialised count stays zero without asking for an unavailable character.

12 | TRACE THE OCCURRENCE COUNT

MatchCount begins at zero before the loop
Position in BANANASubstring of length 1Matches "A"?MatchCount
1"B"FALSE0
2"A"TRUE1
3"N"FALSE1
4"A"TRUE2
5"N"FALSE2
6"A"TRUE3

At nonmatching positions, the count remains unchanged. Position is the loop counter; MatchCount is the summary of successful comparisons.

If you also need matching locations, output Position inside the matching THEN branch. That would report two, four and six, rather than merely their count.

Do not substitute the final Position for MatchCount. Their purposes differ even when they both contain whole numbers.

13 | NORMALISE A CODE WITHOUT CHANGING ITS DIGITS

A school accepts a two-letter prefix in either case, followed by four digit characters. It wants a standard uppercase copy for processing.

DECLARE OriginalCode : STRING
DECLARE NormalCode : STRING
DECLARE Prefix : STRING
DECLARE StudentPart : STRING
OriginalCode ← "st0145"
NormalCode ← UCASE(OriginalCode)
Prefix ← SUBSTRING(NormalCode, 1, 2)
StudentPart ← SUBSTRING(NormalCode, 3, 4)

NormalCode becomes ST0145, Prefix becomes ST and StudentPart remains the string 0145. The leading zero survives because no numeric conversion occurred.

This example assumes the supplied code already meets its defined format and length. UCASE does not verify those rules or prove the student exists.

OriginalCode is retained separately, so its entered form can still be displayed if needed.

14 | TEST LETTER CASE AND POSITION BOUNDARIES

For a case-insensitive Y comparison, test Y and y, a different letter, a longer response such as yes, and a space-padded response. Check the actual intended rule for each.

For a one-position extraction, test position one, the final valid position, zero and one beyond the end. Include an empty string so the no-position path is considered.

For occurrence counting, test no matches, one match, repeated matches and mixed case. An empty source should give zero rather than attempt an invalid extraction.

Use basic examples whose expected positions you can number by hand. A correct uppercase display alone does not prove the occurrence count or bounds checks are correct.

15 | COMMON MISTAKES AND CLEAR EXPLANATIONS

Source changes automatically: conversion returns a result; the assignment destination determines what changes.

Only one side normalised: use a common form for both values when both may vary in case.

Spaces removed by UCASE: conversion is not trimming or full validation.

Single substring treated as CHAR: SUBSTRING returns STRING; UCASE or LCASE preserves the STRING/CHAR kind of its source.

Positions start at zero: use one-based positions for these Cambridge examples.

Loop counter mistaken for matches: count only when the comparison holds.

A useful explanation is: “NormalText holds the uppercase copy, so A and a are compared in the same form. Each matching one-character substring adds one to MatchCount, therefore the summary counts occurrences rather than positions examined.”

The next lesson introduces arithmetic operators, MOD and DIV.

MATCH THE STRING OPERATION TO ITS RESULT

Terminology

Terminology

Uppercase

Capital-letter form, such as A.

Lowercase

Small-letter form, such as a.

UCASE

A Cambridge operation returning its STRING or CHAR argument in uppercase.

LCASE

A Cambridge operation returning its STRING or CHAR argument in lowercase.

Argument

A value supplied to an operation.

Return value

The result an operation produces for another instruction to use.

Case-sensitive comparison

A comparison distinguishing corresponding uppercase and lowercase text.

Case-insensitive comparison

A comparison treating corresponding uppercase and lowercase letters alike.

Normalisation

Converting values to a common form for the intended processing or comparison.

Original text

The source value before any deliberate replacement assignment.

Character position

The numbered place of a character within a string.

One-based positions

The convention in which the first character is at position one.

Single-character substring

A STRING result extracted with length one.

Occurrence

One appearance of a target within the text.

Character scan

Inspecting successive positions in a string.

Match count

The number of positions satisfying the target comparison.

Bounds check

Checking that a requested position lies within the available text.

Loop body

The instruction or group of instructions repeated inside a loop.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. UCASE of Cs Cafe-07 produces which value?
2. Which statements about conversion are correct?
3. Which statements about types are correct?
4. Which comparison accepts y and Y as the same one-character yes response?
5. What does UCASE do to a space-padded y response?
6. In Cs Cafe-07, which are correct?
7. Counting A without distinguishing case in Banana gives which result?
8. Which tests are useful for a string-position scan?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define uppercase and lowercase, and state the Cambridge operations used for them.

2. Explain why UpperText ← UCASE(OriginalText) can preserve the original entered form.

3. Compare case-sensitive and case-insensitive comparison using Y and y.

4. Explain why UCASE of a space-padded y does not equal "Y".

5. Calculate the string at position four and the uppercase character at position two for Cs Cafe-07.

6. Show the matching positions and final count for A ignoring case in Banana.

7. Explain why a one-character substring needs a STRING receiving variable.

8. Suggest bounds tests for inspecting one character and explain the empty-string case.

BUILD IT | COUNT A LETTER WITHOUT DISTINGUISHING CASE

Receive text and count every occurrence of E or e. Preserve the original text, then output it and the count. Use the Cambridge string operations and one-based positions. Write a complete algorithm and trace Cheese, then test a separate empty-string input.

Reveal one possible solution
DECLARE OriginalText : STRING
DECLARE NormalText : STRING
DECLARE Position : INTEGER
DECLARE MatchCount : INTEGER
OUTPUT "Enter text"
INPUT OriginalText
NormalText ← UCASE(OriginalText)
MatchCount ← 0
FOR Position ← 1 TO LENGTH(NormalText)
    IF SUBSTRING(NormalText, Position, 1) = "E"
      THEN
        MatchCount ← MatchCount + 1
    ENDIF
NEXT Position
OUTPUT "Original text: ", OriginalText
OUTPUT "E or e count: ", MatchCount

Cheese becomes CHEESE in NormalText. E occurs at positions three, four and six, so the count is three. OriginalText remains Cheese.

An empty input has length zero; the FOR body runs zero times and MatchCount remains zero. No invalid SUBSTRING call occurs. If the task requires a non-empty input, add its separate presence rule rather than assuming conversion provides it.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 8.1.4E.2 Uppercase, Lowercase and String Positions is coming soon. Complete the matching task and written questions in your exercise book.