In: Computer Science
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".
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.