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.
find failing without guessing what it returnsLevel 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.
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.
Type anything. Every result below is what C++ would give you.
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.
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.
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:
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.
Ask for things: “What is s[4]?” “What is s.length()?” “What does substr(6, 3) give?” “Where does find(‘o’) point?”
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.
Square brackets, index from zero, length one more than the last index. Same rules as level 9.
cin >> stops at the first space, and leaves the rest waiting to confuse you.
Not a yes or no. When it fails it returns string::npos — compare against that, never a guess.
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.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
And this?
What does find give back here?
What does s.find("zzz") return when the text is not there?
← back to the whole C++ track · stuck on anything? ask peter.