fork download
  1. /*
  2.  * Author: ProgramCpp
  3.  */
  4. #include <iostream>
  5. #include <string>
  6.  
  7. class CTokenize
  8. {
  9. std::string string, delimiters;
  10. public:
  11. CTokenize(std::string s, std::string delim) : string(s), delimiters (delim) {}
  12.  
  13. std::string GetNextToken()
  14. {
  15. // Skip delimiters at beginning.
  16. std::string::size_type lastPos = string.find_first_not_of(delimiters, 0);
  17. // Find first "non-delimiter".
  18. std::string::size_type pos = string.find_first_of(delimiters, lastPos);
  19.  
  20. if (std::string::npos != pos || std::string::npos != lastPos)
  21. {
  22. std::string token = string.substr(lastPos, pos - lastPos);
  23. string.erase (lastPos, pos - lastPos);
  24. return token;
  25. }
  26. return "";
  27. }
  28.  
  29. bool HaveMoreTokens()
  30. {
  31.  
  32. return !string.empty();
  33. }
  34.  
  35. };
  36.  
  37. int main(void) {
  38. // your code goes here
  39.  
  40. std::string str("This - should also - tokenized");
  41. std::string str2("demo string _ be _ and printed.");
  42.  
  43. CTokenize tokens(str, std::string("-"));
  44. CTokenize tokens2(str2, std::string("_"));
  45.  
  46. /* walk through the tokens */
  47. while( tokens.HaveMoreTokens() || tokens2.HaveMoreTokens())
  48. {
  49. if(tokens.HaveMoreTokens())
  50. {
  51. std::cout << tokens.GetNextToken() <<std::endl;
  52. }
  53.  
  54. if(tokens2.HaveMoreTokens())
  55. {
  56. std::cout << tokens2.GetNextToken() <<std::endl;
  57. }
  58. }
  59.  
  60. return 0;
  61. }
Runtime error #stdin #stdout 0.22s 3276KB
stdin
Standard input is empty
stdout
This 
demo string 
 should also 
 be 
 tokenized
 and printed.