#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;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RkOjpzdHJpbmcgc3RydG9rX2N1cnJlbnRfc3RyaW5nOwp1bnNpZ25lZCBzdHJ0b2tfY3VycmVudF9wb3M7CgpzdGQ6OnN0cmluZyBzdHJ0b2soY2hhciBkZWxpbWl0ZXIpIHsKICBpZiAoc3RydG9rX2N1cnJlbnRfcG9zID09IHN0ZDo6c3RyaW5nOjpucG9zKSByZXR1cm4gIiI7CiAgdW5zaWduZWQgZW5kID0gc3RydG9rX2N1cnJlbnRfc3RyaW5nLmZpbmQoIGRlbGltaXRlciwgc3RydG9rX2N1cnJlbnRfcG9zICk7CiAgdW5zaWduZWQgbGVuID0gZW5kIC0gc3RydG9rX2N1cnJlbnRfcG9zOwogIHN0ZDo6c3RyaW5nIHJlc3VsdCA9IHN0cnRva19jdXJyZW50X3N0cmluZy5zdWJzdHIoc3RydG9rX2N1cnJlbnRfcG9zLCBsZW4pOwogIHN0cnRva19jdXJyZW50X3BvcyA9IGVuZCAhPSBzdGQ6OnN0cmluZzo6bnBvcyA/IGVuZCsxIDogZW5kOwogIHJldHVybiByZXN1bHQ7Cn0KCnN0ZDo6c3RyaW5nIHN0cnRvayhzdGQ6OnN0cmluZyBzdHIsIGNoYXIgZGVsaW1pdGVyKSB7CiAgc3RydG9rX2N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