Welcome to the World of Strings!

In our previous chapters, we looked at how computers handle numbers and logic. But how does a computer handle text? Whether it is a text message, a username, or a "Game Over" screen, computers use something called Strings. Think of a string as a "string of beads," where every bead is a character (a letter, a number, or even a space). In this chapter, we will learn how to glue strings together, measure them, and cut them into smaller pieces.

What exactly is a String?

A string is an ordered sequence of characters. In programming, we usually put quotation marks around them so the computer knows we are talking about text and not a variable name. For example, \( "Hello" \) is a string, while \( Hello \) (without quotes) might be a variable name.

Important Note: In the AP Computer Science Principles curriculum, we use the AP CSP Exam Reference Sheet notation to understand how to manipulate these strings. If you can master these few rules, you will be able to solve any string problem on the exam!

1. String Concatenation: The Programming "Glue"

Concatenation is a fancy word for joining two or more strings together to create a new, longer string.

In many programming languages, you might see a plus sign \( (+) \) used for this, but the concept is always the same: it places the second string immediately after the first.

Example:
If we have:
\( \text{string1} \leftarrow "Computer" \)
\( \text{string2} \leftarrow "Science" \)
And we concatenate them: \( \text{string1} + \text{string2} \)
The result is: "ComputerScience"

Common Mistake Alert: The computer does not automatically add a space for you! If you want a space between words, you have to include it in one of the strings or add it in the middle: \( "Computer" + " " + "Science" \).

2. Finding the Length of a String

The length of a string is simply the total number of characters it contains. This is done using the \( \text{LENGTH(str)} \) procedure.

How to count correctly:
1. Count every letter.
2. Count every number.
3. Crucial: Count every single space and punctuation mark!

Example:
\( \text{myString} \leftarrow "AP CSP" \)
\( \text{LENGTH(myString)} \)
The result is 6. (A = 1, P = 2, [space] = 3, C = 4, S = 5, P = 6).

Key Takeaway: If a string looks empty but has a space in it, like \( " " \), its length is 1, not 0!

3. Substrings: Cutting the String

A substring is just a smaller part of an existing string. On the AP Exam, you will see a procedure like \( \text{substring(str, start, len)} \). This helps us "extract" a specific piece of text.

Here is how the parameters usually work:
1. str: The string you are cutting.
2. start: The position of the first character you want.
3. len: How many characters you want to take.

The Golden Rule of AP CSP Indexing: Unlike many professional programming languages (like Java or Python) that start counting at 0, AP CSP starts counting at 1.

Example:
Let \( \text{word} \leftarrow "Chocolate" \)
If we run \( \text{substring(word, 1, 4)} \):
- We start at position 1 (the "C").
- We take 4 characters total.
- Result: "Choc"

If we run \( \text{substring(word, 6, 3)} \):
- We start at position 6 (C=1, h=2, o=3, c=4, o=5, l=6).
- We take 3 characters total (l, a, t).
- Result: "lat"

Did you know? If you try to ask for a substring that starts at position 0 or a position longer than the string itself, it will result in an error!

Quick Review & Tips for Success

Don't worry if substrings feel a bit like math at first—it’s just counting! Here is a quick cheat sheet to keep in your head:

  • Indexing: Always start counting at 1.
  • Spaces: They are characters! Always count them when determining length.
  • Concatenation: It just "smooshes" strings together. It doesn't add spaces unless you tell it to.
  • Logic Check: If you use \( \text{substring(str, 1, LENGTH(str))} \), you just get the original string back!

Key Takeaway Summary:

Strings are sequences of characters. To master them for the AP Exam, focus on the AP CSP Reference Sheet rules: \( \text{LENGTH} \) counts everything (including spaces), \( + \) joins strings exactly as they are, and \( \text{substring} \) uses 1-based indexing to pull out specific sections of text. Keep track of your character positions with your fingers if you have to—it works!