Hello Veeky Forums

Hello Veeky Forums

I need to sort a string based on parenthesis.
the given input: "x()y()z(a)" needs to be sorted to "(((xyza)))"

Here is the code I have, but I am getting back "((()))" i understand why this is happening, as making a substring at 1 each time cuts off the character that isn't "(" or an ")"

How am I supposed to keep the other characters, in order, without losing them? I can't use a holding string, since this is a recursive function.

Thanks, lets see if there's anybody relatively smart on here.

Check how many left/right parentheses there are, then strip them and add them to the remainder text.

I don't think that would work recursively.

cuz it doesn't need to be recursive

But in this case, it has to be.... Anything done in a loop can also be done recursively

At the end instead of return rest try putting something l

like return str.substring(0,str.find('(') + (check + str.substring(str.find('(')+1))

there's probably a better way, but if you want to use a holding string you can just use a string pointer as a parameter.

The problem provides the parameter, so I'm not allowed to change it.

I know of these things, but the thing is my class hasn't gone into this detail yet; which wouldn't allow me to use .find.

You're making it far more complicated than it has to be.

so it has to be recursive?

It has to be recursive

You could just put a while loop at the end that checks the index of the first non '(' element and then puts check that is neither '(' nor ')' before it.

It's recursive. No loops can be used.

>lets see if there's anybody relatively smart on here.
seriously?

all you have to do is change the last line to
return str;

>seriously?
That doesn't work.. Did you even read?

Then implement that loop recursively

You can't just switch the position of the character like that, this isn't an array.

A string is exactly an array of chars. You can just use two substrings once you know the number of the first letter/last '('. Use another recursive function to get the number and it's easy.

But since this is sorting, the substrings will get smaller, and anything that calls for an index greater than 1 after a couple passes will throw an out of bounds exception.

I think the question is easier by saying, What do i need to return when they aren't "(" or ")"

& this has to work for any given input, not just the test case.

>But since this is sorting, the substrings will get smaller, and anything that calls for an index greater than 1 after a couple passes will throw an out of bounds exception.
It should be fine if you account for an exception with one if statement or two.

This is one way to do it; I'm not sure how it can be done without another recursive function.

>java

go fucking kill yourself

why would you do it recursively, that's stupid