Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I prevent an endless loop? Printing a string backwards can be done iteratively or recursively. To do it recursively, think

How do I prevent an endless loop?

 

 

 

Printing a string backwards can be done iteratively or recursively. To do it recursively, think of the following specification:

If s contains any characters (i.e., is not the empty string)

???? print the last character in s

???? print s' backwards, where s' is s without its last character

Design program named Backwards.java which prompts the user for a string, then calls method printBackwards() to print the string backwards using ONLY recursion.

 

You can create this program yourself, or use the code on the next page for help:


 

 

// ******************************************************************

//   Backwards.java Uses a recursive method to print a string backwards.

// ******************************************************************

import java.util.Scanner;

 

public class Backwards

{

 

    //--------------------------------------------------------------

    // Reads a string from the user and prints it backwards.

    //--------------------------------------------------------------

    public static void main(String[] args)

    {

            String msg;

            Scanner scan  = new Scanner (System.in);

 

            System.out.print("Enter a string: ");

            msg = scan.nextLine();

            System.out.print("\nThe string backwards: ");

            printBackwards(msg);

            System.out.println();

 

    }

           

           

    //--------------------------------------------------------------

    // Takes a string and recursively prints it backwards.

    //--------------------------------------------------------------

    public static void printBackwards(String s)

    {

            // CHECK FOR BASE CASE

            // FIND THE INDEX OF THE LAST LETTER

            // PRINT THE LAST LETTER TO THE CONSOLE (NOT ON A NEW LINE)

            // CALL THE RECURSION FUNCTION, PASSING A SUBSTRING OF THE STRING

            // WITHOUT THE LAST LETTER

    }

}



Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Simplify each of the following.

Answered: 1 week ago

Question

Evaluate each of the following expressions to six-figure accuracy.

Answered: 1 week ago