Question

In: Computer Science

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.

Solutions

Expert Solution

Answer)
Java program for the recursive function that accepts a string as its argument and prints the string in reverse order:

public class StringReversalRecursion {

   public static void main(String[] args) { // Driver main
  
       String input = "This is a Test";
      
       //Next stage, we pass the input string to the method
       String output = reverseMethod(input); // Receiving the output from the recursive method
       System.out.println("The output string is: " + output);
   }


   //Following method is used for reversal of the stirng and checking if the input stirng is empty or not. Recursion occurs in this method
   public static String reverseMethod(String input)
   {
       if (input.isEmpty()){ // checking if the input is empty or not
       System.out.println("String Reversal is complete.");  
       return input;
       }
       //Recursion calls
       return reverseMethod(input.substring(1)) + input.charAt(0); // recursive calls for the method
   }
}

Output:
String Reversal is complete.
The output string is: tseT a si sihT

Here we are using the string "This is a Test" as input and getting the output:
tseT a si sihT

using recursive method as defined : reverseMethod

**Please Hit Like if you appreciate my answer. For further doubts on the answer please drop a comment, I'll be happy to help. Thanks for posting.**


Related Solutions

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...
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 that takes a String argument and recursively prints out each word in...
Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line. Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string. For example, if the input was “the cat purred”, then the method would print the following to the Java console: the cat purred Challenge Problem Write a recursive method that takes a string as...
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
Python: Write a recursive function that accepts an integer argument, n. The function should display n...
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of...
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically: the first line of the file is a number that indicates the initial value of a Volume object. The remaining lines consist of a single character followed by a space followed by...
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
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#.
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome. If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object. The constructor will use a single stack to evaluate if the String is a palindrome. You must...
/** * Write a recursive function that accepts a stack of integers and * replaces each...
/** * Write a recursive function that accepts a stack of integers and * replaces each int with two copies of that integer. For example, * calling repeatStack and passing in a stack of { 1, 2, 3} would change * the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use * any data structures other than the stack passed in as a parameter. * @param stack */ public static void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT