#include <string>
#include <stdexcept>
#include <cassert>
bool startsWith(const std::string &str,
const std::string &prefix,
std::size_t index = 0) {
return (str.substr(index, prefix.length()) == prefix);
}
int main() {
std::string str = "kobyla ma maly bok";
assert(startsWith(str, "kobyla") == true);
assert(startsWith(str, "ma") == false);
assert(startsWith(str, "ma", 7) == true);
assert(startsWith(str, "kobyla ma maly bok i co z tego") == false);
bool exceptionThrown = false;
try {
startsWith(str, "hehe", 9999);
} catch (std::out_of_range &) {
exceptionThrown = true;
}
assert(exceptionThrown == true);
return 0;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHN0ZGV4Y2VwdD4KI2luY2x1ZGUgPGNhc3NlcnQ+Cgpib29sIHN0YXJ0c1dpdGgoY29uc3Qgc3RkOjpzdHJpbmcgJnN0ciwKICAgICAgICAgICAgICAgIGNvbnN0IHN0ZDo6c3RyaW5nICZwcmVmaXgsCiAgICAgICAgICAgICAgICBzdGQ6OnNpemVfdCBpbmRleCA9IDApIHsKICByZXR1cm4gKHN0ci5zdWJzdHIoaW5kZXgsIHByZWZpeC5sZW5ndGgoKSkgPT0gcHJlZml4KTsKfQoKaW50IG1haW4oKSB7CiAgc3RkOjpzdHJpbmcgc3RyID0gImtvYnlsYSBtYSBtYWx5IGJvayI7CiAgCiAgYXNzZXJ0KHN0YXJ0c1dpdGgoc3RyLCAia29ieWxhIikgPT0gdHJ1ZSk7CiAgYXNzZXJ0KHN0YXJ0c1dpdGgoc3RyLCAibWEiKSA9PSBmYWxzZSk7CiAgYXNzZXJ0KHN0YXJ0c1dpdGgoc3RyLCAibWEiLCA3KSA9PSB0cnVlKTsKICAKICBhc3NlcnQoc3RhcnRzV2l0aChzdHIsICJrb2J5bGEgbWEgbWFseSBib2sgaSBjbyB6IHRlZ28iKSA9PSBmYWxzZSk7CiAgCiAgYm9vbCBleGNlcHRpb25UaHJvd24gPSBmYWxzZTsKICAKICB0cnkgewogIAlzdGFydHNXaXRoKHN0ciwgImhlaGUiLCA5OTk5KTsKICB9IGNhdGNoIChzdGQ6Om91dF9vZl9yYW5nZSAmKSB7CiAgCWV4Y2VwdGlvblRocm93biA9IHRydWU7IAogIH0KICAKICBhc3NlcnQoZXhjZXB0aW9uVGhyb3duID09IHRydWUpOwogIAogIHJldHVybiAwOwp9