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.
| Source | UCASE result | LCASE 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".
| Instruction | Output or assignment result | Text afterwards |
|---|---|---|
| OUTPUT UCASE(Text) | Displays MAYA | Maya |
| UpperText ← UCASE(Text) | Stores MAYA in UpperText | Maya |
| Text ← UCASE(Text) | Stores MAYA in Text | MAYA |
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"
ENDIFThe 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.
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
ENDIFAssume 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: ", MatchCountNormalText 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
| Position in BANANA | Substring of length 1 | Matches "A"? | MatchCount |
|---|---|---|---|
| 1 | "B" | FALSE | 0 |
| 2 | "A" | TRUE | 1 |
| 3 | "N" | FALSE | 1 |
| 4 | "A" | TRUE | 2 |
| 5 | "N" | FALSE | 2 |
| 6 | "A" | TRUE | 3 |
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.