Strings (College Board AP® Computer Science Principles): Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

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, and LEN are 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 string

  • LEN("computer") returns 8

  • LEN("") returns 0 because an empty string contains no characters

  • The 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

CONCAT("Hello", " ", "World")"Hello World"

Substring

Extract a portion of a string by position and length

SUBSTRING("computer", 1, 3)"com"

Length

Return the number of characters in a string

LEN("computer")8

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!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.