Number of words in a string
Mini exercise: count the number of words in a string. bonus points: count the number of characters in each word Note: you can do both in one pass
Short Exercise:
- Create a function to “bisect” a string given a delimiter:
"id=fubar", '='
should returnid
andfubar
. Prototype:
std::pair<std::string, std::string>
bisect(std::string_view str, char delim);
pair
can be found in<utility>
.string_view
instead ofstring const&
allows for use of string literals / C strings without suffering a pointlessstring
allocation.
- Create a function to read the contents of a file at a given path into a vector of strings, each representing a line.
// File:
tag=fubar
xp=523
// Result:
{"tag=fubar", "xp=523"}
Prototype:
std::vector<std::string> readString(std::string_view path);
- Use the above functions to read a configuration file into the fields of a struct. Eg:
struct SaveData
{
std::string username;
s32 windowWidth;
s32 windowHeight;
bool bFullscreen;
bool bVsync;
};