AP Computer Science Principles · Unit 3: Algorithms & Programming
3.4 Strings
Strings are sequences of characters
A string is a sequence of characters, such as letters, numbers, and symbols, used to represent text. Strings are enclosed in quotation marks in most languages. Each character has a position, called an index, and in AP CSP pseudocode, indexing typically starts at 1, not 0.
Common string operations
Programs manipulate strings using built-in procedures: concatenation (joining strings with `+`), finding the length of a string, extracting a substring (a portion of a string), and accessing individual characters by index. These operations let programmers build, search, and modify text data.
Strings in algorithms
Strings are often processed using iteration, looping through each character to search, count, or transform text. Because strings are sequential data, many list-processing techniques (like traversal) apply directly to strings, making them essential for tasks like validating input or parsing data.
Key terms
- String
- A sequence of characters (letters, numbers, symbols) used to represent text.
- Index (AP CSP convention)
- The position of a character in a string; AP CSP pseudocode starts indexing at 1.
- Concatenation
- Joining two or more strings together to form a single, longer string.
- Substring
- A contiguous portion of characters extracted from a larger string.
- Length (string operation)
- An operation that returns the total number of characters in a string.
Try an AP-style question
Code
a ← "sun" b ← "flower" c ← concat(a, b)
What is c?
- "sunflower"
- "sun flower"
- "flowersun"
- "sun"
Show the answer
A. Joined in order.
Practice 8 AP-style questions with instant explanations, flashcards and spaced review.
Study 3.4 free in APrep