This is my first question im sorry if I broke any rules doing so. Anyways my question is about strings. How do I delete parenthesis as well as the text inside of that in a string? for example.. I have a string "example (stuff) example". I want to be able to remove the parenthesis and the characters inside of it every time, whether its like this "(stuff) example example" or this "example example (stuff)". I am also new to c and am using replit as my ide and it doesnt contain an autofill feautre so I dont know what functions im working with making this task hard. If you could also point me in a direction about std::methods please let me know.
Im really new to programming and have usually received help when writing code and am now doing independent work while on break from college.
#include <string>
std::string remove_parentheses(const std::string &str) {
int i = 0;
while(i < str.length()) {
if(str[i] == '(' || str[i] == ')' ) {
str.erase(i,1);
} else{i }
}
return str; // your code here
}
int main() {
std::string str = "Computer systems";
remove_parentheses(str);
return 0;
}
it says..
error: no matching member function for call to 'erase' str.erase(i,1);
input "computer (computer systems) systems"
output "computer systems"
is this not the right way to erase? I cant find the list of functions for strings or std:: functions. thanks for the help!
CodePudding user response:
You can't erase things from a const
reference. The const
modifier is designed specifically to prohibit you from modifying the referent. If you want to modify it in-place, take a non-const reference.
void remove_parentheses(std::string &str)
If you want to make a new string with the modifications intact, take the string by value (incurring a copy when you call the function) and return the new string.
std::string remove_parentheses(std::string str)
CodePudding user response:
Addendum to Silvio Mayolo's answer, but I would strongly recommend outputting a new string as opposed to modifying the existing string in-place. We can use a variable to track levels of nested parentheses, and a std::ostringstream
to efficiently build up a new string minus the parens and their contents.
std::string remove_parentheses(const std::string &str) {
int paren_level = 0;
std::ostringstream ss;
for (auto ch : str) {
if (ch == '(') {
paren_level ;
}
else if (ch == ')' && paren_level > 0) {
paren_level--;
}
else if (paren_level == 0) {
ss << ch;
}
}
return ss.str();
}
CodePudding user response:
You can also start getting used to STL algorithms. Since C 20, you can use std::string::erase_if
, which will basically:
- Walk every character of a string, and erase those that satisfy a condition (the size of the modified string is updated accordingly).
- The condition would be: I've found a close paren, or I have an open paren yet to close. As already explained in @Chris's answer.
- We could work on a copy of the input string, and return that copy.
#include <iostream> // cout
#include <string>
std::string remove_parentheses(std::string str) {
int no_of_open_parens{ 0 };
std::erase_if(str,
[&no_of_open_parens](char c) {
if (c == '(') {
no_of_open_parens ;
} else if (c == ')' and no_of_open_parens > 0) {
no_of_open_parens--;
}
return c == ')' or no_of_open_parens > 0;
}
);
return str;
}
int main() {
std::string in_str = "Computer (computer systems) systems";
auto out_str = remove_parentheses(in_str);
std::cout << out_str << "\n";
}
// Outputs: Computer systems