Question

Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is...

Write a recursive method,

    public static Boolean isPalindrome(String s)

That returns true if s is a palindrome, else false.


0 0
Add a comment Improve this question Transcribed image text
Answer #1
private static boolean isPalindrome(String s)
{   
        if(s.length() == 1 || s.length() == 2 )
        {
            if(s.length()==1)
            {
                return true;
            }

            else
            {
                if(s.charAt(0) == s.charAt(1))
                {
                    return true;
                }
                return false;
            }           
        }

        else
        {
            if(s.charAt(0) == s.charAt(s.length()-1))
            {
                return isPalidrome(s.substring(1, s.length()-1));
            }
            return false;
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is...

    IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is a palindrome or not. A palindrome is a phrase that reads the same forward​and backward; not including punctuation, spaces, or letter case. Some example palindromes include: Just the method please..

  • Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome...

    Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome is a String that reads from right and left the same. An empty String, and a String with one character are Palindrome. public static boolean isPalindrome (String str) {

  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • with "public static" methods for solutions to the following: D. Write a boolean method isValidHex that...

    with "public static" methods for solutions to the following: D. Write a boolean method isValidHex that determines if a single String parameter contains only hexadecimal digits (0-9, a-f, A-F). For example, isValidHex(0) returns true, and isValidHex("A932Bf") returns true, and isValidHex("anythingElse") returns false.   Your solution cannot throw exceptions and must use an if-else block.

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Implement and test the following function://bool ispalindrome(string s)//return true iff s is a palindrome//For example: ispalindrome("RADAR")...

    Implement and test the following function://bool ispalindrome(string s)//return true iff s is a palindrome//For example: ispalindrome("RADAR") return true, //ispalindrome("ABCD") returns false. Enter word to find if the word is palindrome: RADAR The word is RADAR a palindrome Press any key to continue .. Enter word to find if the word is palindrome: ABCDEF The word is ABCDEF is not a palindrome Press any key to continue......

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • Implement the static method declared as follows: /** * Divides {@code n} by 2. * *...

    Implement the static method declared as follows: /** * Divides {@code n} by 2. * * @param n *            {@code NaturalNumber} to be divided * @updates n * @ensures 2 * n <= #n < 2 * (n + 1) */ private static void divideBy2(NaturalNumber n) {...} ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() Implement the static method declared as follows: /** * Checks whether a {@code String} is a palindrome. * * @param s *            {@code String} to be checked * @return true if {@code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT