Write a method that takes a String as input and returns the reverse of that Stri
ID: 3551469 • Letter: W
Question
- Write a method that takes a String as input and returns the reverse of that String. Then write a second method that uses the first method to test whether or not an input String is a palindrome. Your method should return true if the String is a palindrome, and false otherwise. Palindromes were discussed in Exercise 2 of Closed Lab 06, and you should use the same definition here. In addition, write a main method that tests your method with a variety of Strings that should return true and false values.
Explanation / Answer
public class StringReverseWarray {
public static void main( String [] args ) {
String a = "abcdef";
String b = reverse( a );
System.out.println( b );
}
private static String reverse( String a ) {
String result = "";
for (int i = a.length() - 1; i >=0; i--) {
result += a.charAt(i);
}
return result;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.