fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. bool open_bracket = true;
  7. bool terminate = false;
  8.  
  9. size_t pos_open = 0;
  10. size_t pos_close = 0;
  11. size_t last = 0;
  12.  
  13. const char *dvar_hostname = "123]][][[[[456]79[88][[";
  14. string dh(dvar_hostname);//get string value
  15. while(!terminate){
  16. string temp = dh.substr(last);
  17. pos_open = temp.find('[');
  18. pos_close = temp.find(']');
  19.  
  20. terminate = (pos_open == string::npos || pos_close == string::npos);
  21.  
  22. if(!terminate && pos_close > pos_open){
  23. dh.erase(pos_open + last, pos_close - pos_open + 1);
  24. }else if(pos_open != string::npos || pos_close < pos_open){
  25. last += pos_open;
  26. }else if(pos_close != string::npos){
  27. last += pos_close;
  28. }
  29. }
  30.  
  31. dvar_hostname = dh.c_str();//convert back
  32. cout << dvar_hostname;
  33. return 0;
  34. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
123]]79[[