cadence_learn
the_c++_track / level 11 of 15 ▶ 15 min

text is a numbered list.

A string is level 8 and level 9 at the same time — characters that are really numbers, in boxes numbered from zero. Take one apart character by character.

after this level you'll be able to
  • Pull any character or piece out of a string by index
  • Read a whole line of input, spaces included
  • Handle find failing without guessing what it returns
two levels at once

text is a numbered list of numbers

Level 8 told you a letter is stored as a number. Level 9 told you a list is numbered from zero. A string is both of those at the same time, and once you see that, everything it can do stops needing memorising.

a string is levels 8 and 9 together
string s = "Hello world"; — A LIST OF CHARACTERS, WHICH ARE NUMBERS H 0 72 e 1 101 l 2 108 l 3 108 o 4 111 5 9251 w 6 119 o 7 111 r 8 114 l 9 108 d 10 100 INDEX CODE s[0] is 'H' — and 'H' is the number 72 (level 8) s.length() is 11 — one MORE than the last index, exactly like an array. Same counting, same off-by-one.
A string is level 9 and level 8 at the same time: a numbered list, where every box holds a number that everyone agreed to read as a letter.
#include <string> string s = "Hello world"; cout << s.length(); // 11 cout << s[0]; // H — same square brackets as an array cout << s.substr(0, 5); // Hello — start at 0, take 5 cout << s.find("world"); // 6 — the index it starts at

Every number in that block was produced by a real run. Note find hands back an index, not a yes or no — and when it does not find the thing at all it hands back a huge number rather than −1, which catches people out. Compare against string::npos, never against a number you guessed.

◈ ask an ai about this

“How do I work with strings in C++? Explain length, indexing, substr and find with small examples.”

chatgpt ↗ claude ↗

try it yourself

the string workbench

▤ lab 10 · take a string apart

Type anything. Every result below is what C++ would give you.

Try find with something that is not in the text — the answer is not what you would expect.
the three that catch people

quotes, comparing, and reading a whole line

Single quotes are one character, double quotes are text. 'A' is a char. "A" is a string. They are different types and the compiler treats them as such — you met this in level 4 and it comes back constantly here.

You can compare strings with ==, and that is a genuine relief. Verified: string("abc") == string("abc") gives 1. In older C-style text this does not work at all and quietly compares addresses instead — if a tutorial tells you to use strcmp, it is teaching you the old way.

the one that ruins an assignment

cin >> name stops at the first space. Type Maya Patel and name holds Maya, with “Patel” left sitting in the input waiting to ambush your next read. For a whole line you need:

getline(cin, name);

And the classic trap on top of that: mixing cin >> and getline leaves a leftover newline, so the next getline reads an empty line and appears to be skipped. It is not skipped — it read the end of the previous line.

◈ ask an ai about this

“Why does cin >> only read one word, and why does getline sometimes get skipped after it?”

chatgpt ↗ claude ↗

the shape

walking through text

Because a string is a numbered list, level 7’s loop works on it unchanged — and this one shape covers most text questions in a first course:

int vowels = 0; for (int i = 0; i < s.length(); i++) { char c = s[i]; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { vowels++; } }

Count the vowels, reverse it, check if it is a palindrome, find the longest word — all of them are this loop with a different question inside. You are not learning text handling; you are reusing level 7 and level 10 on a list of characters.

do this together

index the sentence

☶ two people · 10 minutes · paper

Write a short sentence with one letter per box, numbered from zero.

person a

Ask for things: “What is s[4]?” “What is s.length()?” “What does substr(6, 3) give?” “Where does find(‘o’) point?”

person b

Answer from the boxes only, never by reading the sentence normally. Spaces are characters too — count them.

Two arguments will happen, and both are worth having: whether length is the same as the last index (it is one more), and whether the space counts (it does). Both are off-by-one errors waiting to happen in an assignment.

what to keep

three things worth remembering

01

It is a list of characters

Square brackets, index from zero, length one more than the last index. Same rules as level 9.

02

getline for whole lines

cin >> stops at the first space, and leaves the rest waiting to confuse you.

03

find gives an index

Not a yes or no. When it fails it returns string::npos — compare against that, never a guess.

check yourself

4 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself4 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

What does this print?

string s = "Hello world"; cout << s.length();
02

And this?

string s = "Hello world"; cout << s.substr(6, 5);
03

What does find give back here?

string s = "Hello world"; cout << s.find("world");
04

What does s.find("zzz") return when the text is not there?

answered: 0 of 4right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →