Skip to main content
AP CSP course

C24 · Topic 3.4 · 40 minutes · AAP-2.D

Reading specified string operations

By the end you can:

  • Use a stated position/count contract.
  • Trace an intermediate string and its length.
  • Test empty and end-of-string cases.

Before this lesson: topics 3.1.

String
A sequence of characters used as text.
Concatenation
Joining text values in order without adding implied separators.

Understand

String operations differ across languages and libraries. A prompt must define any unfamiliar operation, including position numbering and whether a second argument is a count or an end position. Use that supplied definition rather than borrowing a similarly named function’s behaviour from another language.

Our fictional slice(text,start,count)uses one-based positions, a nonnegative count and returns only available characters. Starting at length+1 or asking for 0 characters gives empty text. This is a documented teaching interface, not a claim that the AP reference sheet defines one universal slicing primitive.

Worked example

For “PLANET”, start 2 and count 3 select a three-character substring.

  1. Positions 1…6 contain P,L,A,N,E,T.
  2. Starting at 2 and taking 3 characters selects L,A,N.
  3. The result “LAN” has length 3; start 6,count 4 instead gives only “T”.

Explore each concept

Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.

  • Text digits versus numbers
  • Concatenation and length
  • Literal spaces and leading zeros
Open this example on its own

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to explore: Join exact strings and preserve spaces
  1. Left text (max 30 characters)007Waiting
  2. Right text (max 30 characters)2Waiting
  3. SeparatornoneWaiting

Ready to explore: Join exact strings and preserve spaces

Why: Change an input, predict the first effect, then advance one step.

Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.

Step through and experiment

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to experiment
  1. Input 1PLANETWaiting
  2. Input 22Waiting
  3. Input 33Waiting

Ready to experiment

Why: Predict the first change, then advance one step.

Try this: Try count 0, a count beyond the end, and start one past the last character.

Check a misconception

Claim: The third argument must always be an end index.

Here it is a count; another interface may define a different contract.

Guided practice

Give the result for start 1,count 0 and start 7,count 2 on “PLANET”.

Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.

Independent transfer

Describe how to create “Code: LAN” using the supplied interface.

Exit check

What must be checked before using a familiar substring function from another language?

Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.

Check yourself

  1. Question 1 of 2What does slice("PLANET",4,2)under this contract?

    Select one answer.

  2. Question 2 of 2Which TWO results follow from the stated contract?

    Select exactly two answers (0 of 2 selected).

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring