Re: removing brackets from input
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:CRrSi.51$GB1.46@newsfe06.lga...
"cront" <cront@rocketship.com> wrote in message
news:pan.2007.10.20.18.11.55.305198@rocketship.com...
On Sat, 20 Oct 2007 11:34:21 +0000, robin wrote:
I'm not so clearly about what you want. In the case of "comp.lan(g. (c
++(using PAN)", is "comp.lan" the desired output, or "comp.lan(g. (c+ +"
is?
I want to remove the closest matching bracket-set.
What I would do, then, is simply start looking for a (. Once I found one,
start looking for a ). If I come across another (, save it's position
instead. Once I come acorss a ) I would remove both, then start over
again. Easier than trying to remove them all at once and it should take
care of all situations. Hmm.. even seems rather simple. I think I'll
throw something together real quick. Hope this isn't homework.
Output of the following program is:
Input: comp.lang.(c++) - Output: comp.lang.c++
Input: comp.lang.(c++ - Output: comp.lang.(c++
Input: Bjarn)e St)roustr(up) - Output: Bjarn)e St)roustrup
Input: This ((is) the) way (it (goes))) - Output: This is the way it goes)
#include <iostream>
#include <string>
#include <vector>
std::string RemoveParens( std::string Input )
{
bool Found = true;
while ( Found )
{
Found = false;
std::string::size_type Left = std::string::npos;
std::string::size_type Right = std::string::npos;
for ( std::string::size_type i = 0; i < Input.length(); ++i )
{
if ( Input[i] == '(' )
Left = i;
else if ( Input[i] == ')' && Left != std::string::npos )
{
Right = i - 1;
Input = Input.substr( 0, Left ) + Input.substr( Left + 1 );
Input = Input.substr( 0, Right ) + Input.substr( Right +
1 );
Left = std::string::npos;
Right = std::string::npos;
Found = true;
break;
}
}
}
return Input;
}
int main()
{
std::vector<std::string> Input;
Input.push_back("comp.lang.(c++)");
Input.push_back("comp.lang.(c++");
Input.push_back("Bjarn)e St)roustr(up)");
Input.push_back("This ((is) the) way (it (goes)))");
for ( int i = 0; i < Input.size(); ++i )
std::cout << "Input: " << Input[i] << " - Output: " <<
RemoveParens( Input[i] ) << "\n";
}