#include <string>
#include <iostream>

std::string strtok_current_string;
unsigned strtok_current_pos;

std::string strtok(char delimiter) {
  if (strtok_current_pos == std::string::npos) return "";
  unsigned end = strtok_current_string.find( delimiter, strtok_current_pos );
  unsigned len = end - strtok_current_pos;
  std::string result = strtok_current_string.substr(strtok_current_pos, len);
  strtok_current_pos = end != std::string::npos ? end+1 : end;
  return result;
}

std::string strtok(std::string str, char delimiter) {
  strtok_current_string = str;
  strtok_current_pos = 0;
  return strtok(delimiter);
}

int main() {
  std::string foo = "foo#bar#baz";
  std::cout << strtok(foo, '#') << '\n';
  std::cout << strtok('#') << '\n';
  std::cout << strtok('#') << '\n';
  return 0;
}