February Challenges

Saturday, February 1, 2020 • edited Wednesday, February 26, 2020

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:

  1. Create a function to “bisect” a string given a delimiter: "id=fubar", '=' should return id and fubar. Prototype:
std::pair<std::string, std::string>
bisect(std::string_view str, char delim);
  • pair can be found in <utility>.
  • string_view instead of string const& allows for use of string literals / C strings without suffering a pointless string allocation.
  1. 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);
  1. 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;
};
postschallenges

Primitive Data Types