| | |
Recursion problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 39
Reputation:
Solved Threads: 0
I am writing a method (public static void reverse(String word)) that accepts a String as an argument, then returns the word in reverse order. The easy way would be to just have the recursive method print each letter but that wouldn't be my instructor. He wrote the main and the constructor for the method, its my job to just fill in the method body using recursion to solve this problem. The way he has it written, it appears to me that I have to break the word down, storing each character of the word into a character variable, then adding it to a new string variable in reverse order, however this is not working for me so well. I cannot seem to get the method to pass the value of the String reverseWord once it adds the last character to the next recursion as it works backwards through the characters. All I am getting is the first letter of the string when it does get to the println statement in the main. Here is my code. Thanks in advance for any help or ideas.
public class TestReverse {
public static String reverse(String word) {
String reverseWord = "";
if(word.length() == 0)
return "";
char c = word.charAt(0);
reverse(word.substring(1));
reverseWord += c;
return reverseWord;
}
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("Usage: java TestReverse<word>");
System.exit(1);
}
System.out.println(reverse(args[0]));
}
} You have all the right logic, you're just missing half a line. Looks like a silly mistake on your part.
char c = word.charAt(0);
reverseWord += reverse(word.substring(1));
reverseWord += c;![]() |
Similar Threads
- Recursion help....PLEASE!!! (C++)
- Recursion Problem (Java)
- help with recursion (C++)
- recursion problem... (Java)
- how do you reverse a linked list using recursion and return it as a string? (Java)
- recursion problem (C)
- help with recursion (C)
- C++ Beginner - #include recursion problem (C++)
- Need help with recursion and arrays (C++)
- Recursion (C)
Other Threads in the Java Forum
- Previous Thread: help with string equality
- Next Thread: Ajax upload file in java
Views: 1153 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows





