fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. std::size_t rfind_in_substr(
  6. const std::string& str
  7. , const std::size_t from
  8. , const std::size_t len
  9. , const std::string& sub
  10. ) {
  11. std::size_t res = str.rfind(sub, from+len-sub.size());
  12. return res != string::npos && res >= from ? res : string::npos;
  13. }
  14.  
  15. int main() {
  16. std::string sample("An example with the example word example trice");
  17. // 0123456789012345678901234567890123456789012345
  18. // 0000000000111111111122222222223333333333444444
  19. // substring "ample with the example wo"
  20. std::size_t substr_beg = 5;
  21. std::size_t substr_size = 24;
  22. std::cout << sample.substr(substr_beg, substr_size) << endl;
  23. std::size_t r = rfind_in_substr(sample, substr_beg, substr_size, "example");
  24. std::cout << r << std::endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
ample with the example w
20