fork download
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. unsigned count_occurrences(const std::string& source, char terminal, const std::string& allowed)
  6. {
  7. unsigned count = 0;
  8. std::size_t offset = 0;
  9. while ((offset = source.find(terminal, offset)) != std::string::npos)
  10. {
  11. std::size_t next_off = source.find_first_not_of(allowed, offset+1);
  12. if (next_off != std::string::npos && source[next_off] == terminal)
  13. ++count;
  14.  
  15. offset = next_off;
  16. }
  17.  
  18. return count;
  19. }
  20.  
  21. std::string quoted(const std::string& s)
  22. {
  23. return '"' + s + '"';
  24. }
  25.  
  26. int main()
  27. {
  28. std::string test_cases [] =
  29. {
  30. "+---",
  31. "---+ +---",
  32. "+--- ---+",
  33. "---+",
  34. "+--+--+",
  35. "+-+",
  36. "+--- +",
  37. "+ -+"
  38. };
  39.  
  40. std::cout << std::left;
  41. for (auto& test : test_cases)
  42. std::cout << std::setw(15) << quoted(test) << count_occurrences(test, '+', "-") << '\n';
  43. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
"+---"         0
"---+ +---"    0
"+--- ---+"    0
"---+"         0
"+--+--+"      2
"+-+"          1
"+--- +"       0
"+ -+"         0