fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int count = 0;
  9. string phrase, stringtofind;
  10. bool isamatch;
  11.  
  12. phrase = "ANANAS";
  13.  
  14. stringtofind = "NA";
  15.  
  16. for (size_t i = 0; (i < phrase.size()) && (phrase.size()-i) >= stringtofind.size(); )
  17. {
  18. if (phrase[i] == stringtofind[0])
  19. {
  20. isamatch = true;
  21. for (size_t y = 1; y < stringtofind.size(); ++y)
  22. {
  23. if (phrase[i+y] != stringtofind[y])
  24. {
  25. isamatch = false;
  26. break;
  27. }
  28. }
  29. if (isamatch)
  30. {
  31. ++count;
  32. i += stringtofind.size();
  33. continue;
  34. }
  35. }
  36. ++i;
  37. }
  38.  
  39. cout << "The string NA was found " << count << " times in your phrase";
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
The string NA was found 2 times in your phrase