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 passes itto the method.

Question
Asked Nov 15, 2019
28 views

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 passes it
to the method.

check_circle

Expert Answer

Step 1

A Java program to reverse the given string is as follows,

import java.util.Scanner;

import java.io.*;

 

//Create a class

class StringRev

{

     //Define the main() function

     public static void main(String[] args)

     {

          //Declare String variable

          String str;

         

          //Create Scanner object

          Scanner in=new Scanner(System.in);

         

          //Prompt the user to enter the string

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

         

          //Get the input from the user

          str=in.nextLine();

         

          //Call the method to reverse

          RevString(str);

         

          //Exit the program

          System.exit(0);

     }

     //Define the method to reverse the string

     public static void RevString(String str)

     {

          /*Create for loop to display the string reverse*/

          for (int i=str.length()-1;i>=0;i--)

             

              //Print the character in the string

              System.out.print(str.charAt(i));

     }

}

Screenshot of program #1:

import java.util.Scanner;
import java.io.*;
//Create a class
class StringRev
//Define the main () function
public static void main(String[] args)
//Declare String variable
String str
//Create Scanner object
Scanner in-new Scanner (System.in)
//Prompt the user to enter the string
System.out.print ("Enter the string: ");
1/Get the input from the user
str=in.nextLine ( );
1/Call the method to reverse
RevString(str);
//Exit the program
System.exit(0);
help_outline

Image Transcriptionclose

import java.util.Scanner; import java.io.*; //Create a class class StringRev //Define the main () function public static void main(String[] args) //Declare String variable String str //Create Scanner object Scanner in-new Scanner (System.in) //Prompt the user to enter the string System.out.print ("Enter the string: "); 1/Get the input from the user str=in.nextLine ( ); 1/Call the method to reverse RevString(str); //Exit the program System.exit(0);

fullscreen
Step 2

Screenshot of program #2...

1/Define the method to reverse the string
public static void RevString(String str)
*Create for loop to display the string reverse*/
for (int i-str.length)-1;i>-0;i--)
//Print the character in the string
System.out.print (str.charAt(i))
}
}
help_outline

Image Transcriptionclose

1/Define the method to reverse the string public static void RevString(String str) *Create for loop to display the string reverse*/ for (int i-str.length)-1;i>-0;i--) //Print the character in the string System.out.print (str.charAt(i)) } }

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Engineering

Related Computer Engineering Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: A laser beam 1 mm wide is aimed at a detector 1 mm wide 100 m away on the roof of a building. How mu...

A: Formula for determining the angular diversion of laser before it misses the detector is given below-

question_answer

Q: • Create a Scanner (note which line the user inputs and presses enter)• Asks the user to enter the n...

A: The algorithm for the program would be as below:Prompt the user for the inputAccept the user inputCo...

question_answer

Q: Write the following Book class:A Book has-a title, ISBN number, author and price.o Include the follo...

A: All the lines inside this (/* */) field are comments of this program. Read all comments above the co...

question_answer

Q: how do I Write a String constant consisting of exactly 5 exclamation marks. in python language

A: String Constants: The string constant is nothing but the sequences of characters that is used as dat...

question_answer

Q: perform a What-If Analysis on the amount of Saving based on your own scenario.

A: What-if analysis is a backward processing in which, a specific output is provided to find out the in...

question_answer

Q: a. Assuming that x is 1, show the result of the following Boolean expressions.(true) && (3 &...

A: a. The result for the following Boolean expressions:Assuming x = 1(true) && (3 > 4)3 is n...

question_answer

Q: Write a program in Eclipse that contains 2 methods, main and printLine.

A: Program plan: Import necessary packages.Define a class named “PrintCheck”.Define a method called “pr...

question_answer

Q: For the following problems assume 1 kilobyte (KB) 1024 kilobytes. 1024 bytes and 1 megabyte (MB) For...

A: (a) The last bits of virtual address are called offset. And for a 4K page, the number of bits will b...

question_answer

Q: What is the reduced form of the function F X(XY)+X(X+Y)? a) Y b) X c) 0 d) 1

A: Reduction:The reduced form of the function “F” is computed as follows: