190k views
3 votes
Write a method called printReverse that accepts a String as its parameter and prints the characters in opposite order. For example, a call of printReverse("hello there!"); should print the following output: !ereht olleh If the empty string is passed, no output is produced. Your method should produce a complete line of output.

User PAStheLoD
by
7.8k points

1 Answer

2 votes

Answer:

//Method to reverse user text

public static void printReverse(String text) {

//Create a variable to hold the reversed string

//Initialize it to an empty string

String reversedText = "";

//Create a loop that cycles through each character in the text string

//Prepend each character to the reversedText string

for(int i=0; i < text.length(); i++) {

reversedText = text.charAt(i) + reversedText;

}

//Display the reversed text

System.out.println(reversedText);

}

Step-by-step explanation:

Explanation has been given in the code in form of comments.

Hope this helps!

User OGHaza
by
8.3k points