fork(1) download
  1. #include <string>
  2. #include <stdexcept>
  3. #include <cassert>
  4.  
  5. bool startsWith(const std::string &str,
  6. const std::string &prefix,
  7. std::size_t index = 0) {
  8. return (str.substr(index, prefix.length()) == prefix);
  9. }
  10.  
  11. int main() {
  12. std::string str = "kobyla ma maly bok";
  13.  
  14. assert(startsWith(str, "kobyla") == true);
  15. assert(startsWith(str, "ma") == false);
  16. assert(startsWith(str, "ma", 7) == true);
  17.  
  18. assert(startsWith(str, "kobyla ma maly bok i co z tego") == false);
  19.  
  20. bool exceptionThrown = false;
  21.  
  22. try {
  23. startsWith(str, "hehe", 9999);
  24. } catch (std::out_of_range &) {
  25. exceptionThrown = true;
  26. }
  27.  
  28. assert(exceptionThrown == true);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty