hi , i need to implement the method public String reverse(string string) ...im only allowed to use the methods concant, length, and charAt from the String object and i have to use recursion( no for loops)
I know how to to this with the substring method but in this case i cant use it
This sounds like a homework problem. Based on the methods provided, I think the general idea you're expected to use is to take the first character of the string and call reverse on the rest, then concatenate the reversed rest of the string with the first character (on the end, of course). Obviously, your base case (which you must check before the rest of the method) is that the reverse of an empty or single-character string (where str.length() < 2) is itself.
Note that we wouldn't do this in the real world: Java is not good with recursion, and this wouldn't work well for long strings.
No, it's not. I was just pointing out that the StringBuilder technique will not work in all real-world situations, either (although of course it's much better for many). For extremely large strings, you'd want a stack (and the string in a queue to start with), unless the string wouldn't fit in memory even once, in which case you'd need to use a stream to some other storage medium that could be read backwards.
Of course, this is where lazy evaluation comes into its own — the recursive function actually works quite well if evaluated lazily I don't think that's really possible in Java, though, alas.
Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.