Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code in java! Write a recursive static method named reverseString that

ID: 3685698 • Letter: W

Question

Write the code in java!

Write a recursive static method named reverseString that accepts an integer and returns the integer as a string in reverse order (without using any String or Character methods). For example, 4295 would be returned as “5924”.

package prob1;

public class RecursionTester {

public static void main(String[] args) {

System.out.println("Test 2a: reverseString(): " + reverseString(6));

System.out.println("Test 2b: reverseString(): " + reverseString(29));

System.out.println("Test 2c: reverseString(): " + reverseString(1234567));

public static String reverseString(int x){

    //Write code here!!!!!!

Explanation / Answer

public class RecursionTester {

   public static void main(String[] args) {

       System.out.println("Test 2a: reverseString(): " + reverseString(6));

       System.out.println("Test 2b: reverseString(): " + reverseString(29));

       System.out.println("Test 2c: reverseString(): "
               + reverseString(1234567));
   }

   public static String reverseString(int x) {
       if (x == 0) // base case
           return "";
       return "" + x % 10 + reverseString(x / 10);

   }
}

/*

Output:

Test 2a: reverseString(): 6
Test 2b: reverseString(): 92
Test 2c: reverseString(): 7654321

*/