fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. bool contains( std::string haystack, const std::string& needle ) {
  5. haystack.insert( 0, " " );
  6. haystack += ' ';
  7. auto pos = haystack.find( needle );
  8.  
  9. if( pos == haystack.npos ) {
  10. std::cout << "a\n";
  11. return false;
  12. }
  13.  
  14. if( haystack[pos - 1] != ' ' ) {
  15. std::cout << "b\n";
  16. return false;
  17. }
  18.  
  19. if( haystack[pos + needle.length()] != ' ' ) {
  20. std::cout << "c\n";
  21. return false;
  22. }
  23.  
  24. return true;
  25. }
  26.  
  27. int main() {
  28. std::cout << std::boolalpha << contains( "a!hi", "!hi" );
  29. return 0;
  30. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
b
false