Uncategorized
Practice
Uncategorized

Strings: AP® Computer Science Principles Review

Strings appear throughout the AP CSP exam—in pseudocode trace questions, free-response algorithm writing, and data-processing scenarios. Get comfortable with concatenation, SUBSTRING, and string comparison, and you’ll handle every string question the exam throws at you.

What Strings Are and How They’re Stored

A string is a sequence of characters—letters, digits, punctuation, spaces—treated as a single data unit. In AP pseudocode, strings are enclosed in quotation marks: "Hello, World!", "12345", "" (empty string), "CS Rocks".

Note that "12345" as a string is different from the number 12345. String characters that look like numbers don’t behave like numbers—you can’t add "123" + "456" and get 579. You get "123456" (concatenation).

Strings are stored as sequences in memory, with each character occupying a position. Like lists in AP pseudocode, string characters are referenced using 1-based indexing—the first character is at position 1, not 0.

String Length, Indexing, and Character Access

Before you can manipulate strings, you need to know how long they are and how to access individual characters.

String length is the total number of characters, including spaces and punctuation. In AP pseudocode: LENGTH("Hello") returns 5, LENGTH("Hello World") returns 11 (including the space), and LENGTH("") returns 0.

Characters in a string are accessed by their position, starting at index 1. The string "STRINGS" has characters at positions 1–7: S (1), T (2), R (3), I (4), N (5), G (6), S (7).

In AP pseudocode, individual characters are accessed using the SUBSTRING function: SUBSTRING("COMPUTER", 1, 1) returns "C", SUBSTRING("COMPUTER", 4, 4) returns "P", and SUBSTRING("COMPUTER", 8, 8) returns "R".

A common exam pitfall is mixing up 0-based indexing (used in Python and Java) with the 1-based indexing in AP pseudocode. When the AP exam says “the character at position 3,” they mean the third character, not the fourth.

String Concatenation and Common Operations

String concatenation combines two or more strings into a single string. In AP pseudocode, this uses the + operator: "Hello" + " " + "World" produces "Hello World". If firstName ← "Alice" and lastName ← "Wonder", then firstName + " " + lastName produces "Alice Wonder".

Operation Pseudocode Example Result When to Use It
Substring SUBSTRING("COMPUTERS", 2, 4) "OMP" (characters at positions 2, 3, 4) Extracting parts of a string (file extensions, formatted data)
String comparison "apple" = "apple" → true; "apple" = "Apple" → false Case-sensitive character-by-character comparison Validation, authentication, conditional branching on text input
Concatenation "Hello" + " " + "World" "Hello World" Building output strings, combining user input with literal text

String comparison is case-sensitive by default. "A" and "a" are different characters. When comparing user input to a stored value, convert both to the same case first using UPPER() or LOWER() if case shouldn’t matter.

String Concatenation in Loops and Building Strings Dynamically

In practical programming, you build strings iteratively—adding characters or substrings one at a time inside a loop. Two classic examples:

Example 1: Reversing a string.

Assign original ← "HELLO"
Assign reversed ← ""
Assign i ← LENGTH(original)
REPEAT UNTIL (i < 1)
{
    Assign reversed ← reversed + SUBSTRING(original, i, i)
    Assign i ← i - 1
}
DISPLAY reversed

This builds "OLLEH" by accessing characters from position 5 down to position 1 and concatenating each one to the reversed string.

Example 2: Filtering characters.

Assign original ← "Hello World"
Assign noSpaces ← ""
Assign i ← 1
REPEAT UNTIL (i > LENGTH(original))
{
    IF (SUBSTRING(original, i, i) ≠ " ")
    {
        Assign noSpaces ← noSpaces + SUBSTRING(original, i, i)
    }
    Assign i ← i + 1
}
DISPLAY noSpaces

This produces "HelloWorld" by skipping spaces. The pattern—initialize an empty string, loop through characters, selectively concatenate—appears frequently in AP FRQ questions about text processing.

String Comparison and Practical Applications

String comparison goes beyond checking equality. Strings compare lexicographically—character by character using their underlying numeric values (like ASCII values). Uppercase letters sort before lowercase, digits sort before letters, and comparison stops at the first differing character.

Practical string manipulation scenarios tested on the AP exam include input validation (checking if a username is the correct length or has a required format like containing “@” for an email), data extraction (parsing a record to pull out a specific field or extracting a file extension), text transformation (converting input to a standard format or removing specific characters), pattern detection (checking if a string contains a keyword or counting occurrences of a character), and output formatting (building a sentence by concatenating variables and literal text).

Common String Operations in AP Pseudocode

Here’s a reference table of the string operations you’ll encounter on the AP exam:

Concept Pseudocode Explanation
String length LENGTH(str) Returns the number of characters (1-indexed count)
Substring / character access SUBSTRING(str, start, end) Returns characters from position start through end (1-indexed, inclusive)
Concatenation str1 + str2 Combines two strings, left before right
Convert to uppercase UPPER(str) Returns the string with all characters in uppercase
Convert to lowercase LOWER(str) Returns the string with all characters in lowercase

The AP exam defines the exact pseudocode syntax in the problem statement. Always read provided function definitions carefully—they’re your guarantee of what’s available and how it works. Don’t assume a function behaves exactly like Python or Java.

Test-Taking Strategies

Three core mnemonics. For indexing: “AP starts at 1.” All AP pseudocode uses 1-based indexing—lists, strings, everything. If you learned Python’s 0-indexing first, this is the most important habit to override on exam day. For concatenation order: “Left then right.” "Hello" + "World" is "HelloWorld", not "WorldHello". For SUBSTRING parameters: think “Start, End, Inclusive.” SUBSTRING("COMPUTER", 2, 4) returns the 3 characters at positions 2, 3, and 4: "OMP".

Cross-topic connections. Strings are sequences just like lists, and both use 1-based indexing in AP pseudocode—if you can iterate a list, you can iterate a string character by character. String manipulation is often delegated to helper procedures, making validation routines (“does this string contain an ‘@’?”) classic procedure examples. Strings are also an abstraction: a sequence of characters stored as numeric codes (like ASCII) that you interact with as readable text. Password validation, input sanitization, and data format checking—all real-world security techniques—use string operations.

Common question formats. Character/substring identification questions give you a string and ask what SUBSTRING returns or what character is at a specific position—these test indexing accuracy. Trace-through questions have pseudocode that manipulates a string in a loop and you trace what the string becomes (common scenarios include reversing a string, removing characters, or counting occurrences). Write-pseudocode questions give you a problem (“write pseudocode to validate that a password is at least 8 characters and contains at least one digit”) and ask for a complete algorithm using string operations.

Frequent student mistakes. Using 0-based indexing: AP pseudocode strings are 1-indexed, so SUBSTRING("CAT", 1, 1) returns "C". Treating numeric strings as numbers: "123" + "456" is "123456", not 579. Assuming case-insensitive comparison: "Hello" ≠ "hello" in AP pseudocode—always convert to the same case with UPPER() or LOWER() when case shouldn’t matter. Treating an empty string as having length 1: "" has LENGTH 0. Forgetting concatenation is left-to-right: result ← result + nextChar appends to the right; to prepend (add to the front), write result ← nextChar + result.

Test-day shortcuts. For trace-through questions, build a two-column table—iteration number and current string value—making it impossible to lose track of what the string contains mid-loop. For SUBSTRING confusion, apply the function to a simple test string like "ABCDE" and verify the result makes sense: SUBSTRING("ABCDE", 2, 4) should give "BCD". For character access, visualize the string as a row of labeled boxes: [A:1][B:2][C:3][D:4][E:5]. For building strings in a loop, clarify the starting value (usually result ← "") and which end new characters attach to before writing the loop.

Time management. MCQ substring and indexing questions: 30–60 seconds—sketch the string with position labels and read off the answer rather than working it out mentally. Trace-through loop questions: 2–3 minutes—use a table, and if you find yourself going in circles, restart the table fresh. FRQ string algorithm questions: 8–12 minutes—outline before writing (“1. Loop from position 1 to LENGTH(str). 2. Extract character with SUBSTRING. 3. Check condition. 4. Concatenate to result.”) then convert to pseudocode. If stuck, start with the simplest version: can you extract one character? Yes. Can you check one condition? Yes. Extend from there.

Ready to practice? Try Albert free.

Build real confidence with thousands of adaptive practice questions across AP®, SAT®, ACT®, and core subjects — aligned to what you’re studying right now.

Practice for AP® Computer Science Principles