Java Write a valid Java method called printReverse that take
Java
Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
Solution
A valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String.
public class Reverse extends Program {
public void run()
String str = readLine(\"Type a string to reverse: \");
int index = str.length() - 1;
while(index >= 0) {
print(str.substring(index, index + 1));
index--;
}
}
}
need to introduce a new method, which we\'ll call printReverse.
public class ReverseRecur extends Program {
public void run() {
String line = readLine(\"Type a string to reverse: \");
printReverse(line);
}
public void printReverse(String str) {
}
}
recursive version of Reverse.
public class ReverseRecur extends Program {
public void run() {
String line = readLine(\"Type a string to reverse: \");
printReverse(line);
}
public void printReverse(String str) {
if(!str.equals(\"\")) {
print(str.substring(str.length() - 1));
printReverse(str.substring(0, str.length() - 1));
}
}
}
One more example:
import java.util.Scanner;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = \"\";
Scanner in = new Scanner(System.in);
System.out.println(\"Enter a string to reverse\");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println(\"Reverse of entered string is: \"+reverse);
}
}

