AP Computer Science Principles · Study guide

Strings

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Strings

What a string is, how to join strings end to end, how to take part of a string, and how to build a new string one piece at a time.

1What a string is

A string is an ordered sequence of characters, written in double quotes: "New York". Letters, digits, punctuation, and spaces are all characters. Like a list, a string has a length and its characters have positions counted from 1.

city ← "New York"
DISPLAY(LENGTH(city))
DISPLAY(city[5])

This displays 8, because the space counts, and then Y: character 4 is the space, so the Y is character 5.

The reference language has no string operators of its own. Programs work with strings through procedures, named commands that return a value. This guide uses two common ones:

ProcedureReturns
concat(str1, str2)a new string: the characters of str1 followed by the characters of str2
substring(str, start, length)a new string: length characters of str, starting at position start

Different programs may name them differently. Always read how the procedure is described before using it.

2Joining strings

String concatenation joins two or more strings end to end to make a new string.

Figure 1. The characters keep their order; the second string starts right after the last character of the first.
ExpressionValueWhy
concat("Sun", "day")"Sunday"end to end, in order
concat("day", "Sun")"daySun"swap the order, get a different string
concat("5", "3")"53"digits in quotes are characters, not numbers
concat("Mia", "Park")"MiaPark"no space unless you join one in
concat("", "go")"go"the empty string changes nothing

To join more than two strings, nest the calls: concat(concat("Mia", " "), "Park") is "Mia Park". Joining never does arithmetic: concat("5", "3") is "53", while the number expression 5 + 3 is 8.

3Taking part of a string

A substring is part of an existing string.

word ← "notebook"
part ← substring(word, 5, 4)
DISPLAY(part)
DISPLAY(word)
DISPLAY(LENGTH(word))
Figure 2. A substring is a copy of part of an existing string. Taking it does not change the original.

The program displays book, then notebook, then 8: the original string is unchanged and still 8 characters long. substring("notebook", 1, 4) is "note". Asking for characters past the end, such as 4 characters starting at position 6, is an error, just like a list index that is too large.

The key idea

A string is an ordered sequence of characters, and spaces count. Concatenation joins strings end to end, in order, into a new string. A substring is a copy of part of an existing string; the original stays the same.

4Building a string in a loop

Start with the empty string "" and join on one piece each pass. Which side you join on decides the order of the result. This loop puts each new letter in front, so it reverses the word:

word ← "stop"
result ← ""
FOR EACH letter IN word
{
  result ← concat(letter, result)
}
DISPLAY(result)
letterresult after the pass
"s""s"
"t""ts"
"o""ots"
"p""pots"

It displays pots. Change the line to result ← concat(result, letter) and it would display stop again.

5Worked examples

Worked Example 1 Evaluate

Make a username

What is displayed?

first ← "Jordan"
last ← "Rivera"
user ← concat(substring(first, 1, 1), last)
DISPLAY(user)
  1. 1
    Work inside out. substring(first, 1, 1) is the first character of Jordan: J.
  2. 2
    Then concat("J", last) joins it to the front of Rivera.
Answer: JRivera
Worked Example 2 Evaluate

Two pieces of one word

What is displayed?

s ← "MIDNIGHT"
DISPLAY(substring(s, 4, 5))
DISPLAY(substring(s, 1, 3))
  1. 1
    Number the characters: M1 I2 D3 N4 I5 G6 H7 T8.
  2. 2
    Start at 4 and take 5: N, I, G, H, T, which is NIGHT.
  3. 3
    Start at 1 and take 3: MID.
Answer: NIGHT then MID.
Worked Example 3 Evaluate

Digits as text

What is displayed?

a ← "7"
b ← "11"
c ← concat(a, b)
d ← concat(b, a)
DISPLAY(c)
DISPLAY(d)
  1. 1
    a and b are strings, so concat joins characters; nothing is added.
  2. 2
    c is "711" and d is "117": order matters.
Answer: 711 then 117, not 18.
Worked Example 4 Build a string

Initials

What does this display?

phrase ← "keep it simple"
initials ← ""
initials ← concat(initials, substring(phrase, 1, 1))
initials ← concat(initials, substring(phrase, 6, 1))
initials ← concat(initials, substring(phrase, 9, 1))
DISPLAY(initials)
  1. 1
    Characters 1, 6, and 9 of keep it simple are the first letters of its three words: k, i, s. (The spaces are characters 5 and 8.)
  2. 2
    Each line joins one letter to the end of initials, which started empty.
Answer: kis

6Common mistakes

Common Mistake 1 · Expecting a space to appear

concat({q('Mia')}, {q('Park')}) is {q('MiaPark')}. If you want a space, join one in.

Common Mistake 2 · Adding digits in quotes

{q('7')} is a character. Joining it to {q('11')} gives {q('711')}, never 18.

Common Mistake 3 · Skipping spaces when counting

Every space has a position and adds 1 to the length. Write out the positions under the string when it matters.

Common Mistake 4 · Thinking a substring shortens the original

A substring is a new string made from part of the old one. The old string keeps every character.

Explain a string result like this

“substring(first, 1, 1) gives the first character, J. concat joins it end to end with Rivera, so the result is JRivera.”

Evaluate the innermost call first, name each piece, and say in which order the pieces are joined.