fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. std::string strtok_current_string;
  5. unsigned strtok_current_pos;
  6.  
  7. std::string strtok(char delimiter) {
  8. if (strtok_current_pos == std::string::npos) return "";
  9. unsigned end = strtok_current_string.find( delimiter, strtok_current_pos );
  10. unsigned len = end - strtok_current_pos;
  11. std::string result = strtok_current_string.substr(strtok_current_pos, len);
  12. strtok_current_pos = end != std::string::npos ? end+1 : end;
  13. return result;
  14. }
  15.  
  16. std::string strtok(std::string str, char delimiter) {
  17. strtok_current_string = str;
  18. strtok_current_pos = 0;
  19. return strtok(delimiter);
  20. }
  21.  
  22. int main() {
  23. std::string foo = "foo#bar#baz";
  24. std::cout << strtok(foo, '#') << '\n';
  25. std::cout << strtok('#') << '\n';
  26. std::cout << strtok('#') << '\n';
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
foo
bar
baz