Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I wrote a recursive method in java to detect if a String text is a palindrome. I have implemented it using the built-in method called
I wrote a recursive method in java to detect if a String text is a palindrome. I have implemented it using the built-in method called substring, but I cannot use that and need a work-around. Can I please get some assitance on how to implement this function without using ".substring()"? Below is my code. Thank you.
/** * Returns a boolean value representing whether the passed in character * sequence is a valid palindrome. A palindrome is defined as such: * A word, phrase, or sequence that reads the same backward as forward. * * Palindromes are recursively defined as such: * Case 1: An empty string or single character is considered a palindrome * Case 2: A string is a palindrome if and only if the first and last * characters are the same and the remaining string is also a palindrome * * For the purposes of this method, two characters are considered * 'the same' if they have the same primitive value. You do not need * to do any case conversion. Do NOT ignore spaces. * * This method must be computed recursively! Failure to do so will result * in zero credit for this method. * * @param text The sequence that will be tested * @return Whether the passed in word is a palindrome * @throws IllegalArgumentException if text is null */ public static boolean isPalindrome(String text) { if (text == null) { throw new IllegalArgumentException("text cannot be null"); } if (text.equals("") || text.length() == 1) { return true; } if (text.charAt(0) == text.charAt(text.length()-1)) { return isPalindrome(text.substring(1, text.length() - 1)); //FIX CUZ CANT USE SUBSTRING!!!!!!!! } return false; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started