Question

[10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For...

[10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For example, reverse("UBC-O") displays O-CBU. Use a helper method to improve the performance of your program.
Write a test program that prompts the user to enter a string and displays its reversal.

MUST HAVE A HELPER METHOD TO IMPROVE PERFORMANCE.

FOR JAVA

THANK YOU!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class StringReverse {

    public static void printReverseHelper(String s, int index) {
        if (index < s.length()) {
            printReverseHelper(s, index + 1);
            System.out.print(s.charAt(index));
        }
    }

    public static void printReverse(String s) {
        printReverseHelper(s, 0);
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String line = in.nextLine();
        printReverse(line);
    }
}

Add a comment
Know the answer?
Add Answer to:
[10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • write a java program :- Use a recursive method that can take an arbitrarily long string...

    write a java program :- Use a recursive method that can take an arbitrarily long string and reverse the order of its characters. Example: "Real men don't eat quiche." would be ".ehciuq tea t'nod nem leaR" must use RECURSION to solve this challenge. make sure to comment the code thoroughly.

  • Write a program in MIPS to accept a string from a user, reverse that string and...

    Write a program in MIPS to accept a string from a user, reverse that string and print it out to the user. You must write a procedure that performs the reversal task. Example: Please Enter a String: Hello How Are You Reversed String: uoY erA woH olleH

  • Java Program Write a recursive method that takes a string and return a string where every...

    Java Program Write a recursive method that takes a string and return a string where every character appears twice. Output must match exactly as below: only two LL's since there is already two of the character For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it. must be recursive!!!

  • Please help me with this question Write a recursive method that returns the largest integer in...

    Please help me with this question Write a recursive method that returns the largest integer in an array. Write a test program that prompts the user to enter a list of eight integers and displays the largest largest element. Thank you! and can you give details so I know what is going on?

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • (Process a string) Write a program that prompts the user to enter a string and displays...

    (Process a string) Write a program that prompts the user to enter a string and displays its length and its first character. java StringLength Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic? The string is of length 88 and the first character is D import java.util.Scanner; class StringLength{    public static void main (String args[]){        Scanner input = new Scanner(System.in);        System.out.println("Enter a string:");        String ans = input.nextLine();        System.out.println("The string...

  • Please use Java. Write a non-recursive method that uses a stack to check whether its string...

    Please use Java. Write a non-recursive method that uses a stack to check whether its string parameter is a palindrome. Write a program to test and demo your method. Fibonacci Numbers Write a method that uses a stack to determine the desired Fibonacci number. Write a program to test and demo your method. Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT