I NEED COMENTS PLEASE!!!!! Please code this in JAVA and USE mutliple COMMENTS ex
ID: 3724042 • Letter: I
Question
I NEED COMENTS PLEASE!!!!!
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. 1. 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. 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/function 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 you 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(); // reading the string from console
while(!s.equalsIgnoreCase("done")) {//loop to run continuously till user enters done
if(reverseString(s).equalsIgnoreCase(s)) {//calling reverseString to get reverse string and comparing with user input
System.out.println("Given string is palindrome"); //if matches then palindrome
} else {
System.out.println("Given string is NOT a palindrome");//if not matches, not palindrome
}
System.out.println("Enter the string to reverse, or ‘done’ to exit: ");
s = scan.next();//reading the input from user
}
System.out.println("Thanks for using my program");
}
/**
* Recursive Method to reverse the user input string.
* @param original
* @return String
*/
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:
done
Thanks for using my program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.