|
-
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.
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
|