|
-
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));
}
}
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
|