-
April 6th, 2009, 09:16 PM
#1
reverse a string
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
-
April 7th, 2009, 01:25 AM
#2
Re: reverse a string
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.
-
April 7th, 2009, 05:17 AM
#3
Re: reverse a string
Originally Posted by Twey
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.
In the real world, you'd probably put it in a StringBuilder, call reverse() on it, and pull it out again...
Controlling complexity is the essence of computer programming...
B. Kernighan
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.
-
April 7th, 2009, 05:21 AM
#4
Re: reverse a string
Well, that works fine if the string fits comfortably into memory a few times, yes
-
April 7th, 2009, 04:10 PM
#5
Re: reverse a string
Is recursion with String going to use less memory? - every call copies the string (on concatenation), whereas reverse in StringBuilder does it all in place...
And simple truth miscalled simplicity...
W. Shakespeare
Last edited by dlorde; April 7th, 2009 at 04:23 PM.
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.
-
April 8th, 2009, 07:08 AM
#6
Re: reverse a string
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.
Last edited by Twey; April 8th, 2009 at 07:10 AM.
-
April 8th, 2009, 08:37 AM
#7
Re: reverse a string
OIC. Fair enough
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...
D. Knuth
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|