|
-
June 11th, 2012, 11:25 AM
#1
mirror Strings using recursive method
Hi,
I have been tasked to a string recursive problem for my computer science homework. The task is to write a recursive method with the following signature: public String mirror(String str) that returns a copy of the string, followed by all the letters except the last in reverse order. For example, if given "Saturday", you should return "SaturdayadrutaS".
I have written a java code that does the mirror but I seem to get the complete string together. Hope someone can give me some hints on how to achieve the result using recursion.
This is the code:
public String mirror(String str){
int length = str.length();
//char secondLastChar = Character.toLowerCase(str.charAt(length-1));
if(length <= 1){
return str;
}
else{
char c = str.charAt(length-1);
return c + mirror(str.substring(0,length-1));
}
}
-
June 11th, 2012, 05:04 PM
#2
Re: mirror Strings using recursive method
Are you sure that's exactly what your homework question says?
-
June 12th, 2012, 12:13 AM
#3
Re: mirror Strings using recursive method
Yes. I am sure. Is it possible to achieve that using recursion?
-
June 12th, 2012, 04:32 AM
#4
Re: mirror Strings using recursive method
Not if the recursive method has to have the signature shown.
You can do it if your mirror method can call a method with a different signature which is recursive but why would you even want to do this with recursion? There are much better and much easier ways of achieving the required result.
-
June 12th, 2012, 05:32 AM
#5
Re: mirror Strings using recursive method
 Originally Posted by keang
Not if the recursive method has to have the signature shown.
You can do it if your mirror method can call a method with a different signature which is recursive but why would you even want to do this with recursion? There are much better and much easier ways of achieving the required result.
I think it is possible (and quite easy) with a method with that signature. But I agree with you that there are better solutions without using recursion.
@raylistic87, your attempt is not too far from a recursive solution, but you are only getting the string reversed. Just remember that each character in the original string (except for the last one) goes twice in the result.
Have a look at the example you were given:
Code:
Saturday => SaturdayadrutaS
Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).
-
June 12th, 2012, 08:22 AM
#6
Re: mirror Strings using recursive method
Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).
Yes you are right, my mistake.
-
June 12th, 2012, 11:13 AM
#7
Re: mirror Strings using recursive method
 Originally Posted by jcaccia
I think it is possible (and quite easy) with a method with that signature. But I agree with you that there are better solutions without using recursion.
@raylistic87, your attempt is not too far from a recursive solution, but you are only getting the string reversed. Just remember that each character in the original string (except for the last one) goes twice in the result.
Have a look at the example you were given:
Code:
Saturday => SaturdayadrutaS
Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).
Thanks! I manage to solve it!
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
|