The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.4

3.4 Strings

Strings are ordered sequences of characters. The exam tests the concept — concatenation and substrings — rather than specific string functions.

What you need to know

  • A string is an ordered sequence of characters (letters, digits, spaces, symbols), written in quotes: "Hello".
  • Concatenation joins strings end to end. "AP" + "CSP" → "APCSP". Note there's no automatic space.
  • A substring is any contiguous run of characters within a string. "Comp" is a substring of "Computer"; "Cptr" is not.
  • Strings can be compared, searched, and have their length measured. The exact operation names vary by language; the CED expects the concepts, not a specific syntax.
  • A string containing digits, like "42", is text, not a number — concatenating "4" + "2" gives "42", not 6.
  • Strings are immutable in many languages: operations produce a new string rather than changing the original.

Worked example

first ← "Grace"
last ← "Hopper"
full ← first + " " + last
DISPLAY(full)

Concatenation with an explicit space in the middle → Grace Hopper. Without the + " " +, the output would be GraceHopper. Questions love that missing space.

Exam tip: When the exam shows string operations, it defines them in the question (e.g., "the procedure SUBSTRING(str, start, end) returns…"). Read that definition carefully — in particular, whether the end index is included — and apply it literally rather than assuming a language you know.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • The CED's essential knowledge on strings is minimal: they're ordered sequences of characters, and you can concatenate and take substrings. Everything else — length, indexing, searching — the exam defines inside the question if it's needed.
  • Concatenation with + works on strings in AP pseudocode. Combining a string and a number is not defined on the reference sheet; if a question does it, it will explain the behavior.
  • A substring must be contiguous. "cat" is a substring of "concatenate" (positions 4–6). "cnt" is not, even though all three letters appear in order.
  • The empty string "" is a valid string with zero characters. Concatenating it changes nothing. It's often the starting value when building a string in a loop.
  • When a question defines a string procedure — say, SUBSTRING(str, start, length) or SUBSTRING(str, start, end) — the parameters matter enormously. One version takes a length, one takes an end index, and the end may or may not be inclusive. Read the definition every time.
  • Building a string character by character in a loop is the standard pattern for reversing or filtering text: start with "", add one character per iteration.

Mistakes that cost points

  • Adding a space that isn't there. "a" + "b" is "ab". If a space is needed it must be concatenated explicitly.
  • Treating a numeric string as a number. "4" + "2" is "42" if both are strings. Arithmetic isn't happening.
  • Assuming a substring definition. Don't apply Java's or Python's rules. Use whatever the question says, including whether indices start at 1 and whether the end is inclusive.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 The variable word holds the string "program". Which of the following is a substring of word?
  1. A "pgm"
  2. B "gram"
  3. C "margorp"
  4. D "prog ram"
Show answer

Answer: B. A substring must be a contiguous run of the original characters. "gram" is the last four characters in order.

Q2 What is displayed by the following code?
a ← "12"
b ← "3"
DISPLAY(a + b)
  1. A 15
  2. B 123
  3. C 36
  4. D An error, because strings cannot be added
Show answer

Answer: B. Both values are strings, so + concatenates them: "12" followed by "3" is "123".

Key vocabulary

String
an ordered sequence of characters
Concatenation
joining two strings end to end
Substring
a contiguous sequence of characters within a string