Strings (College Board AP® Computer Science Principles): Revision Note
Character & string operations
What is a string?
A string is an ordered sequence of characters
Characters include letters, digits, spaces and special symbols
Strings are used to represent text-based data such as names, messages and passwords
A string can contain any number of characters, including zero (an empty string)
Each character in a string has a position, starting at index 1 in AP CSP exam notation
Examiner Tips and Tricks
SUBSTRING,CONCAT, andLENare not on the AP Exam Reference Sheet.On the exam, string operations are described in prose or defined as custom procedures within the question.
The function names used below are illustrative to help you understand the concepts.
Concatenation
Concatenation joins two or more strings together end to end to produce a new string
Concatenating
"hello"and"world"produces"helloworld"Concatenation does not automatically add spaces, so a space character must be included explicitly if needed
Substring extraction
Substring extraction retrieves a portion of a string by specifying the starting position and the number of characters to extract
SUBSTRING("computer", 1, 3)returns"com"(starts at position 1, extracts 3 characters)SUBSTRING("computer", 5, 4)returns"uter"(starts at position 5, extracts 4 characters)
String operations in practice
The following program takes a user's full email address and extracts the username portion (everything before the
@symbol at position 8), then combines it with a greeting.
email ← "[email protected]"
username ← SUBSTRING(email, 1, 7)
greeting ← CONCAT("Welcome, ", username)
DISPLAY(greeting)
Output:
Welcome, student
String length
The
LEN()function returns the number of characters in a stringLEN("computer")returns8LEN("")returns0because an empty string contains no charactersThe length of a string can be used to determine valid index ranges or to check whether a string meets a minimum or maximum character requirement
Operation | Purpose | Code example |
|---|---|---|
Concatenation | Combine two or more strings into one |
|
Substring | Extract a portion of a string by position and length |
|
Length | Return the number of characters in a string |
|
Examiner Tips and Tricks
Pay close attention to index values and character counts in substring questions. The AP exam uses 1-based indexing, so the first character is at position 1. A common mistake is miscounting the starting position or the number of characters to extract. When concatenating, remember that no space is added automatically, so if a question expects
"hello world"you need to concatenate"hello"," ", and"world"separately.For the AP Create Performance Task, string operations are a useful way to demonstrate how your program manipulates and processes text-based data — be prepared to explain what string operations your program uses and why.
Worked Example
A program contains the following code segment.
word1 ← "sun"
word2 ← "flower"
result ← CONCAT(word1, word2)
DISPLAY(result)
What is displayed as a result of executing the code segment?
(A) "sun flower"
(B) "sunflower"
(C) "flower sun"
(D) "sun"
[1]
Answer:
(B) "sunflower" [1 mark]
CONCAT(word1, word2)joins"sun"and"flower"end to end without adding a space, producing"sunflower"
Unlock more, it's free!
Was this revision note helpful?