fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::string without_brackets(std::string str, char beg = '(', char end = ')') {
  5. auto last = str.find_last_of(end);
  6. auto first = str.find_first_of(beg);
  7.  
  8. if(last != std::string::npos) {
  9. str.erase(str.begin()+last);
  10. }
  11. if(first != std::string::npos) {
  12. str.erase(str.begin()+first);
  13. }
  14.  
  15. return str;
  16. }
  17.  
  18.  
  19. using namespace std;
  20.  
  21. int main() {
  22. cout << without_brackets("abc)") << endl
  23. << without_brackets("(abc") << endl
  24. << without_brackets("(abc)") << endl
  25. << without_brackets("abc") << endl;
  26. return 0;
  27. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
abc
abc
abc
abc