Getting Started
From the name in your contacts to the text of this very sentence, computers must be able to store and manipulate text. To a computer, text is not a collection of words but a sequence of individual characters. This fundamental data type, known as a string, is the building block for processing nearly all human-readable information in software.
What You Should Be Able to Do
Explain how strings are used to represent textual data in a program.
Predict the result of combining strings using concatenation.
Write expressions to extract a portion of a string, known as a substring.
Trace simple algorithms that iterate through the characters of a string to process them.
Develop simple algorithms to perform common text manipulations.
Key Concepts & Application
The Core Idea
In computing, a string is a sequence of characters, which can include letters, numbers, spaces, and symbols. Think of a string like beads on a cord: each bead is a single character, and the entire cord of beads represents the string. The order of the characters is fixed and matters. For example, the string "stop" is entirely different from "pots," even though they use the same characters.
This simple representation is incredibly powerful. It allows a program—a collection of instructions that performs a specific task—to handle everything from a user's password to the content of a web page. By treating text as a structured sequence, we can write an algorithm, which is a finite set of instructions that accomplishes a task, to search, modify, and combine text in predictable ways.
Logic & Application
Working with strings involves a few core operations. The most common are joining strings together, extracting a piece of a string, and finding out how long a string is.
Key String Operations
| Operation | Pseudocode Example | Description |
|---|---|---|
| Length | LEN("Hello") | Returns the number of characters in a string. This example would return 5. |
| Concatenation | CONCAT("Hello", "World") | Joins two or more strings end-to-end to create a new string. This returns "HelloWorld". |
| Substring | SUBSTRING("APCSP", 3, 2) | Extracts a part of a string. It takes three arguments: the string, a starting position (1-based), and the number of characters to extract. This returns "CS". |
Annotated Pseudocode Examples
These examples show how string operations are used within an algorithm to manage textual data, which is information that is collected and used for reference or analysis.
Example 1: Creating a Full Name
This algorithm combines a first and last name with a space in between.
// Assign initial string values to variables
firstName <- "Grace"
lastName <- "Hopper"
// Concatenate the first name with a space
fullName <- CONCAT(firstName, " ")
// Concatenate the result with the last name
fullName <- CONCAT(fullName, lastName)
// Display the final, combined string
DISPLAY(fullName)
Output: "Grace Hopper"
Example 2: Extracting an Area Code
This algorithm extracts the first three characters from a 10-digit phone number string.
// Store a phone number as a string
phoneNumber <- "202-555-0181"
// Extract the first 3 characters starting at position 1
areaCode <- SUBSTRING(phoneNumber, 1, 3)
// Display the extracted part
DISPLAY(areaCode)
Output: "202"
Tracing & Analysis
Logic Trace
Let's trace the execution of the "Creating a Full Name" algorithm to see how the fullName variable changes. A trace is a manual walkthrough of an algorithm's steps.
| Line Number | Action | firstName | lastName | fullName |
|---|---|---|---|---|
| 1 | firstName <- "Grace" | "Grace" | (not set) | (not set) |
| 2 | lastName <- "Hopper" | "Grace" | "Hopper" | (not set) |
| 3 | fullName <- CONCAT(firstName, " ") | "Grace" | "Hopper" | "Grace " |
| 4 | fullName <- CONCAT(fullName, lastName) | "Grace" | "Hopper" | "Grace Hopper" |
| 5 | DISPLAY(fullName) | "Grace" | "Hopper" | "Grace Hopper" |
The final displayed value is "Grace Hopper". Notice that concatenation does not change the original strings (firstName, lastName); it produces a new one that is then stored in the fullName variable.
Key Terminology & Logic
This table summarizes the core vocabulary and pseudocode commands related to strings.
| Term / Command | Description |
|---|---|
String | A data type representing a sequence of characters. |
Concatenation | The operation of joining two or more strings end-to-end. |
Substring | A portion of a larger string. |
LEN(aString) | A function that returns the number of characters in aString. |
CONCAT(string1, string2) | A function that returns a new string by joining string1 and string2. |
SUBSTRING(str, start, len) | A function that returns a new string by extracting len characters from str, beginning at the start position (1-based index). |
Core Concepts & Terminology
String: A sequence of characters used in computing to represent text. Strings are a fundamental form of data.
Concatenation: The process of appending one string to the end of another to form a new, longer string.
Substring: A part or segment of an existing string. Extracting a substring does not modify the original string.
Traversal: The process of accessing each character of a string one by one, from beginning to end, typically to perform some operation.
Core Logic: Concatenation: This operation creates a new string by joining others.
part1 <- "Welcome " part2 <- "to CSP!" greeting <- CONCAT(part1, part2) // The variable 'greeting' now holds "Welcome to CSP!"Core Logic: Substring: This operation extracts a portion of a string without changing the original.
word <- "Computer" section <- SUBSTRING(word, 1, 4) // The variable 'section' now holds "Comp"
Core Skill Check
Logic Tracing: What is the final value of
resultafter this pseudocode runs:result <- SUBSTRING(CONCAT("Network", "Protocols"), 8, 5)?Debugging: A programmer wants to get the last character of the string
word <- "Data". They writelastChar <- SUBSTRING(word, 5, 1). Explain why this will cause an error.Application: Describe how a word processing application might use the
LEN()function to provide a character count to the user.
Common Misconceptions & Clarifications
Confusing Numbers and Number Characters: The string
"123"is not the same as the numeric value123. You cannot perform mathematical addition on it.CONCAT("5", "2")results in the string"52", not the number 7.Assuming 0-based Indexing: While many programming languages start counting positions from 0, the pseudocode used for the exam uses 1-based indexing. The first character of a string is at position 1.
Believing Concatenation Modifies Originals: The
CONCAToperation is non-destructive. It produces a new string and leaves the original strings unchanged. To save the result, you must assign it to a variable.Misinterpreting
SUBSTRINGArguments: The commandSUBSTRING(string, start, length)uses a starting position and a length (the number of characters to grab), not a starting position and an ending position.
Summary
Strings are the primary way that computer programs represent and manipulate text. They are an ordered sequence of characters, which allows algorithms to perform precise operations. The most fundamental of these operations are concatenation (joining strings), substring extraction (slicing a piece from a string), and finding the length of a string. Mastering these simple building blocks is essential for developing software that can process user input, display meaningful messages, and manage the vast amount of textual data that defines our digital world.