Recap
- A string is an ordered sequence of characters: letters, digits, spaces, punctuation.
""is the empty string (length 0)."7"is a string;7is a number.- Concatenation joins strings end to end and makes a new string. Order matters.
- A substring is a part of an existing string. The original does not change.
- In this pack: positions start at 1,
substring(text, start, length)andconcat(a, b). On the exam, use the definition the question supplies.
Key relationships
| Task | Expression | Value |
|---|---|---|
| Join two strings | concat("sun", "shine") |
"sunshine" |
| Join three strings | concat(concat("a", "b"), "c") |
"abc" |
| First character | substring("Mina", 1, 1) |
"M" |
| Last two characters of “2011” | substring("2011", 3, 2) |
"11" |
| Grow a string in a loop | result ← concat(result, "ab") |
"ababab" after three passes |
| Empty string | "" |
length 0 |
Assumptions
- Strings are not changed by
substringorconcat; they return new strings. - Substring requests in the examples stay inside the string.
- A question may use different names or a different counting convention. Follow the question.
Method
- Write the string with each character numbered above it.
- Work out each
substringby marking its characters. - Evaluate inner
concatcalls first. - Count the characters in your answer and compare with the expected length.
Mistakes to avoid
- Reversing the order of a
concat. - Treating the third argument as an end position.
- Ignoring spaces inside quotes.
- Believing
substringedits the original. - Treating
"12"as the number 12.
Worked mini-example
s ← "banana"
part ← substring(s, 2, 3)
DISPLAY(concat(part, "!"))
Output: ana!
"banana" is b-a-n-a-n-a. Three characters from position 2 are a, n, a, so part is "ana", and joining "!" gives "ana!".
A second mini-example: a short code from a name
name ← "Okafor"
tag ← concat(substring(name, 1, 2), substring(name, 5, 2))
DISPLAY(tag)
Output: Okor
Positions in "Okafor": O-1, k-2, a-3, f-4, o-5, r-6. Two characters from position 1 are "Ok"; two characters from position 5 are "or". Joined: "Okor", four characters, which equals 2 + 2 ✓.
Quick self-check
- What is
concat("go", "al")? - What is
substring("sunshine", 4, 3)? - Are
"tea"and"eat"the same string?
Answers
"goal"."shi"(positions 4, 5 and 6 are s, h, i).- No. The same characters in a different order are different strings.
Where marks are won and lost
String questions are usually short evaluation tasks: given a string variable and one or two procedures defined in the question, state the final string. Marks go to candidates who track positions carefully and who remember that concatenation order matters. A second pattern asks which of four code segments builds a required string. Check the lengths first: if the target has 9 characters, a correct answer must produce 9. In the Create task, strings often appear as messages or labels in your own procedure.
Back to the full study guide for worked examples and explanations.