C++ Strings Quiz: find, stoi, Formatting and String View
Test your C++ string skills with 14 questions and three programs on find and npos, substr, char conversions, stoi, parsing input, formatting and string view.
- Course: C++ study plan
- Module: Strings
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Strings is the checkpoint that closes the Strings module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: std::string as a value type, the find family and npos, slicing and replacing, characters as small integers and the <cctype> cast, stoi and std::from_chars, splitting and validating input, manipulators and std::format, and std::string_view with its lifetime rule.
How it works. Fourteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What does
std::string::findreturn when nothing matches, and how do you test for it? - Why must a
charbe cast tounsigned charbefore callingstd::isalpha? - What is the difference between
std::stoi("42abc")andstd::from_charson the same text? - Which stream manipulators are sticky, and which one applies to the next insertion only?
- What does
std::setprecision(2)print for1234.5withoutstd::fixed? - When does a
std::string_viewdangle, and what is the one rule that prevents it? - Why does a replace-all loop advance by the replacement's length rather than the match's?
The three programs are a word-frequency count, a fixed-width report and a run-length codec; get the reading idiom right before the logic.
Common questions
What does std::string::find return when nothing matches?
It returns std::string::npos, the largest std::size_t. Test with pos != std::string::npos; find(x) >= 0 is always true, because the result is unsigned.
What is the difference between std::stoi("42abc") and std::from_chars on the same text?
std::stoi("42abc") returns 42 and ignores the rest unless you check how many characters it consumed. std::from_chars also parses 42 but leaves ptr pointing at the a, so testing ptr == last reports that the whole token was not a number.
What does std::setprecision(2) print for 1234.5 without std::fixed?
It prints 1.2e+03. Without std::fixed the precision counts significant digits, and two significant digits of 1234.5 need scientific notation. With std::fixed the same setting prints 1234.50.
Why does a replace-all loop advance by the replacement's length rather than the match's?
So the next search starts after the text just inserted. If the replacement contains the search string — replacing "a" with "aa" — restarting at the match position finds the insertion again and the loop never ends.
Exercises
Word frequency
Read all of standard input. A word is a maximal run of letters and digits (std::isalnum with the unsigned char cast), converted to lower case; every other character separates words. Print each distinct word with its count as <word> <count>, one per line, in ascending std::string order, then total=<n> distinct=<m>.
Input: any number of lines. Output: one line per distinct word, then the summary.
Example: input
Hello, hello! HELLO world.
World 42 42
→ output
42 2
hello 3
world 2
total=7 distinct=3
A std::map<std::string, int> keeps the words in order for you.
Temperature report
Read an integer N (at least 1) and then N lines. Each line is a city name — one or more words separated by single spaces — followed by two decimals, the maximum and minimum temperature. Parse from the right: the last two tokens are the numbers and everything before them is the name.
Print a header and one row per city with std::format: header {:<14}{:>7}{:>7}{:>7} with City, Max, Min, Range; rows {:<14}{:>7.1f}{:>7.1f}{:>7.1f} with the name, max, min and range (max − min). Then print Widest range: <city> (<range with one decimal>) — the first city on a tie.
Input: N, then N lines. Output: N + 2 lines.
Example: input
3
New York 28.5 19.0
Oslo 12.3 -3.5
Lima 21 15
→ output
City Max Min Range
New York 28.5 19.0 9.5
Oslo 12.3 -3.5 15.8
Lima 21.0 15.0 6.0
Widest range: Oslo (15.8)Run-length codec
Read lines until end of input. Each line is encode <text> or decode <text>; the text contains no spaces.
encode: the text is letters only. Replace every maximal run of one character with<count><char>:aaabcc→3a1b2c.decode: the text should be a sequence of<count><char>groups where the count is one or more digits and the char is a letter; expand it:3a1b2c→aaabcc. Counts may have several digits (12x). If the text is not well-formed — a count with no letter after it, a letter with no count before it, a count of zero, or an empty text — printinvalid.
Use c - '0' to accumulate counts, std::to_string to emit them, and the <cctype> functions with the unsigned char cast.
Input: any number of command lines. Output: one line per command.
Example: input
encode aaabcc
decode 3a1b2c
decode 3
→ output
3a1b2c
aaabcc
invalidIn this module: Strings
← std::string_view — a window onto text · Built-in arrays and std::array →