Question

In: Computer Science

Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...

  • Code in Java
  • Write a recursive method, reverseString, that accepts a String and returns the String reversed.
  • Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList.
  • Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the ArrayList.

Input Format

a series of Strings followed by the word done.

Constraints

none

Output Format

The strings reversed and in reverse order

Sample Input 0

Keyboard
Done

Sample Output 0

Enter a series of Strings, each on a different line. When done, type "Done".
draobyeK

Sample Input 1

Mouse
Keyboard
Done

Sample Output 1

Enter a series of Strings, each on a different line. When done, type "Done".
draobyeK
esuoM

Sample Input 2

Mouse
Monitor
Computer
Headphones
Done

Sample Output 2

Enter a series of Strings, each on a different line. When done, type "Done".
senohpdaeH
retupmoC
rotinoM
esuoM

Sample Input 3

Done

Sample Output 3

Enter a series of Strings, each on a different line. When done, type "Done".

Solutions

Expert Solution

I have implemented reverseString() and reverseArrayList() method which will reverse the string and reverse array list which contains number of strings respectively.

1> public String reverseString(String str) :- This method uses the substring() and charAt() method of String class and return reverse string by calling itself. (Recursion).

2> public void reverseArrayList(ArrayList<String> list) :- This method get the first item then remove it each time untill the list does not becomes empty then after add the items that are removed. Thus, our list is reversed using this method in recursive mannar.

Program:-

This program get the strings from user until the user does not enter "Done". After that reverseString() method called which reverse the each string of array list and then call the reverseArrayList() method which will reverse our array list.

import java.util.ArrayList;
import java.util.Scanner;

public class TestReverse {

    /**
     * This method returns the reverse string
     * @param str to be reverse
     * @return reverse string
     */
    public String reverseString(String str) {
        
        /*
            base condition
            untill the string become empty
        */
         if (str.isEmpty())
            return str;
        
        //Calling method Recursively by cgetting string fromm index 1 each time and adding  oth index character.
        return reverseString(str.substring(1)) + str.charAt(0);
    }

    /**
     * This method reverse the array list
     * @param list to be reverse
     */
    public void reverseArrayList(ArrayList<String> list) {
        
        
        /*
            base condition 
            return when list become empty
        */
        if(list.isEmpty())
            return;
        
        // now store first string from the array lsit
        String firstString = list.get(0);
        
        // then remove first string
        list.remove(list.get(0));
        
        reverseArrayList(list);
        
        // now after getting the laset element after recursion add it to the fitst
        list.add(firstString);
        
    }
    
    
    public static void main(String[] args) {
        
        // create an object of TestReverse class for callinfg reverseString() and  reverseArrayList()
        TestReverse reverse = new TestReverse();
        
        // create an object of Scanner class for user input
        Scanner sc = new Scanner(System.in);
        
        // create an array list which stores iputted string
        ArrayList<String> list = new ArrayList<>();
        
      
        
        // get the string from the user
        String string = sc.nextLine();
        
        // ask string from user untill the user does not enter Done
        while(!string.equals("Done")){
            
            // add the string into the array list
            list.add(string);
            
            // again get the string from the user
            string = sc.nextLine();
        }
        
        // now reverse string of array list
        for(int i=0; i<list.size(); i++){
            
            // call the reverrseString(string) method which will reverse string using recursion
            String reverseString = reverse.reverseString(list.get(i));
            
            // now set the reverseString in array list
            list.set(i, reverseString);
            
        }
        
        // now call the reverseArrayList() method
        reverse.reverseArrayList(list);
            
        System.out.println("\nEnter a series of Strings, each on a different line. When done, type \"Done\".");
        
        // print each reversed string and reverse array list
        for(String strReversed : list)
            System.out.println(strReversed); 
        
    }

}

Output:-

1> Sample output 1

2> Sample output 2

3> Sample output 3

4> Sample output 4

I hope you will understand above program and how to reverse string and arraylist using recursion.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT