Write a recursive method to determine whether a given string is a palindrome

For example, rotor is a palindrome, but motor is not.

Write a recursive method to determine whether a given string is a palindrome

Recursion and Loop A String is said to be Palindrome if it is equal to itself in reverse order. You can use this logic to check if String is Palindrome or not.

Recursive algorithms

There are two common ways to find if a given String is Palindrome or not in Java, first by using for loop, also known as iterative algorithm and second by using recursion, also known as recursive algorithm.

The crux of this problem lies in how do you reverse String in Java? If both are equal then given String is Palindrome otherwise it's not. Also whether your solution is iterative or recursive will also determine by implementing this logic.

If you reverse String using for loop then it become an iterative solution and if you reverse String using recursion then it become a recursive solution. In general, recursive solution are short, readable and more intuitive but subject to StackOverFlowError and that's why not advised to be used in production system.

You should always be using iterative solution in production, unless your programming language supports tail recursion optimization e. Scala which eliminates risk of StackOverFlowError by internally converting a recursive solution to an iterative one.

String, array, linked list, binary tree, networking etc. How to check if String is Palindrome using Recursion Easiest way to find if a given String is Palindrome or not is by writing a recursive function to reverse the String first and then comparing given String with the reversed String, if both are equal then given String is palindrome.

This logic is coded in our static utility method isPalindrome String input method. This method calls another method called reverse String strwhich is responsible for reversing given String using recursion. A recursive function needs a based case to terminate recursion and produce result.

In this program, base case is empty Stringif String is empty just return itself and don't call the method. We are also using substring method from java.

String class to reduce the String in every call so that our solution reaches to base case after every call. If you remember the structure is quite similar to our earlier solution of problem how to find if a number is Palindrome in Java. Only thing different there was the logic to reverse numbers.

How to check if String is Palindrome using Iteration In our second solution we will try to solve this problem by using for loop. If you use any loop e. This solution uses extra space in form of StringBuilder which may or may not be allowed sometime.

You can check with your interviewer whether you can use StringBuffer to reverse String in Java or not. He may not allow directly using reverse method but he may allow it for String concatenation. The logic of this solution is coded in method checkPalindrome String text.

This method first create reversed String by iterating over String in reverse order e. You can easily do this in a for loop because it allows you to control index. It's actually quite similar to how you loop over array because String is a character array and you can get character array from String by calling toCharArray method.

Once you reverse the given String, its all about check if two Strings are equal to each other using equals method, if it returns true then String is Palindrome. This example program contains two methods, isPalindrome and checkPalindromfirst method check if String is Palindrome using loop while second method checks if String is Palindrome using recursion.

We also have JUnit tests written to unit test our solution. I have two test methods testPalindromeRecursive and testPalindromefirst one tests our recursive solution and second one tests our iterative algorithm.

You can see input there, "madam" is a Palindrome and our solution should return true for it.Download Palindrome program class file.. Palindrome Java program output: Java palindrome program without reversing a string.

Another method to check palindrome: We can compare characters at the beginning and at the end of a string and move towards the middle of the string. Use this method to easily check that a required querystring both exists and is of a certain type.

This lets you fire off a few checks in your page_load and then write the rest of the code on the page safe in the knowledge that the querystring exists, has a value and can be parsed as the intended data type.

write a recursive method to determine whether a given string is a palindrome

Jun 27,  · Solution 1: How to check if String is Palindrome using Recursion Easiest way to find if a given String is Palindrome or not is by writing a recursive function to reverse the String first and then comparing given String with the reversed String, if both are equal then given String is palindrome.

A string is palindrome, if string remains same after reversing sequence of it's benjaminpohle.com example, "tenet" is a palindrome string whereas "mango" is not a palindrome string.

We can check whether a string is palindrome or not using recursion by breaking this problem into a smaller problem.

perlretut. NAME; DESCRIPTION; Part 1: The basics. Simple word matching; Using character classes; Matching this or that; Grouping things and hierarchical matching.

Creating a recursive method for Palindrome. Ask Question. It does not make sense to include case insensitivity in the recursive method since it only needs to be done once, Determine whether or not a given string is a valid palindrome or not.

JS. 1.

Java Basics Exercises - Java Programming Tutorial