This is what I have right now public class Reverser1 { public static String reve
ID: 3729701 • Letter: T
Question
This is what I have right now
public class Reverser1 {
public static String reverse (String original) {
return reverse(original, 0, original.length() -1);
}
private static String reverse (String source, int lowIndex, int highIndex) {
if (lowIndex < highIndex)
return source.charAt(highIndex) + reverse(source, lowIndex+1, highIndex-1) + source.charAt(lowIndex);
else
return "";
}
public static void main(String[] args) {
System.out.println(reverse("camouflage"));
}
}
right now this prints out camouflage backwards, i cannot figure out how to make it a scanner so what ever words i type in it should print out backwards. Help please
Explanation / Answer
// FOR Scanner
import java.util.Scanner;
public class Reverser1 {
public static String reverse (String original) {
return reverse(original, 0, original.length() -1);
}
private static String reverse (String source, int lowIndex, int highIndex) {
if (lowIndex < highIndex)
return source.charAt(highIndex) + reverse(source, lowIndex+1, highIndex-1) + source.charAt(lowIndex);
else
return "";
}
public static void main(String[] args) {
// prompting for input
System.out.print("Enter a word: ");
// taking input
Scanner sc = new Scanner(System.in);
String word = sc.next();
// calling method with that word
System.out.println(reverse(word));
}
}
/*
SAMPLE OUTPUT
Enter a word: CheggIndia
aidnIggehC
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.