Please code this in JAVA and USE mutliple COMMENTS explaining the code with // D
ID: 3723374 • Letter: P
Question
Please code this in JAVA and USE mutliple COMMENTS explaining the code with //
Description: Reversing strings and checking for palindromes (Using The Module One Readings) In this program you will design and write a computer program to execute the following tasks. Basically, it uses a recursive algorithm to reverse strings until the user types "done" as the entry string Read in a string from the user. Prompt her with something like "Enter the string to reverse, or ‘done' to exit. If the user enters “done" then exit. 1. 2. Use a recursive algorithm and implementation to reverse the string 3. Compare the original and reversed strings to see if they are the same (i.e. whether they are palindromes). Print out whether they are palindromes or not. The program should loop through these three steps until the user indicates they are done. For example, if the user entered "Finally" the program would reverse the string so that it was "yllaniF" and would display that. And then indicate whether they are the same (in this case, no). Then it would prompt for the next string Hints: View the string as String or an array of characters, depending on the language you are using, and your preferences. You can use the recursion to move one character of the array from the beginning to the end. Then you can call the method/functiorn recursively to reverse the rest of the string What if the string is of even length? What if it is odd? What do you need to do to handle those cases (if anything)? You do not need to do the string comparison recursively. You may do it in whatever fashion vou choose . .Explanation / Answer
ReverseStringRecursive.java
import java.util.Scanner;
public class ReverseStringRecursive {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string to reverse, or ‘done’ to exit: ");
String s = scan.next();
while(!s.equalsIgnoreCase("done")) {
if(reverseString(s).equalsIgnoreCase(s)) {
System.out.println("Given string is palindrome");
} else {
System.out.println("Given string is NOT a palindrome");
}
System.out.println("Enter the string to reverse, or ‘done’ to exit: ");
s = scan.next();
}
System.out.println("Thanks for using my program");
}
public static String reverseString(String original) {
if(original.length() == 1) {
return original;
} else {
return original.charAt(original.length()-1)+reverseString(original.substring(0,original.length()-1));
}
}
}
Output:
Enter the string to reverse, or ‘done’ to exit:
madam
Given string is palindrome
Enter the string to reverse, or ‘done’ to exit:
loop
Given string is NOT a palindrome
Enter the string to reverse, or ‘done’ to exit:
done
Thanks for using my program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.