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

Write a program called PensionContributionCalculator that reads the monthly salary and age in int of an employee. Your program shall calculate the employee's, employer's and total contributions in double ; and print the results rounded to 2 decimal places. For examples, Enter the monthly salary:

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

For example, rotor is a palindrome, but motor is not. How can you use recursion to determine whether a word is a palindrome? Let's start by understanding what's a base case. Consider the word a. In fact, we don't have to think of palindromes as actual words in the English language or whatever language you'd like to consider.

We can think of a palindrome as just any sequence of letters that reads the same forward and backward, such as xyzyzyx. We call a sequence of letters a string.

So we can say that any string containing just one letter is by default a palindrome. Now, a string can contain no letters; we call a string of zero letters an empty string. An empty string is also a palindrome, since it "reads" the same forward and backward. So now let's say that any string containing at most one letter is a palindrome.

That's our base case: What if the string contains two or more letters?

Follow by Email

Here's where we'll have our recursive case. Consider the palindrome rotor. Its first and last letters are the same, and this trait must hold for any palindrome. On the other hand, if the first and last letters are not the same, as in motor, then the string cannot possibly be a palindrome.

So now we have a way to declare that a string is not a palindrome: We can think of this situation as another base case, since we have the answer immediately.

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

Going back to when the first and last letters are the same, what does that tell us? The string might be a palindrome. Then again, it might not be. In the string rater, the first and last letters are the same, but the string is not a palindrome. Suppose we strip off the first and last letters, leaving the string ate.

Then the first and last letters of this remaining string are not the same, and so we know that rater is not a palindrome. So here's how we can recursively determine whether a string is a palindrome.

If the first and last letters differ, then declare that the string is not a palindrome. Otherwise, strip off the first and last letters, and determine whether the string that remains—the subproblem—is a palindrome.

Declare the answer for the shorter string to be the answer for the original string. Once you get down to a string with no letters or just one letter, declare it to be a palindrome.

Here's a visualization of that for two words that we discussed: How would we describe that in pseudocode? If the string is made of no letters or just one letter, then it is a palindrome. Otherwise, compare the first and last letters of the string. If the first and last letters differ, then the string is not a palindrome.

Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string.Jun 08,  · Another interesting array interview question, where you need to treat the array as Set.

Your task is to write a function in your favorite language e.g. Java, Python, C or C++ to return the intersection of two sorted arrays. Array C/C++ Programs.

C Program to find sum of elements in a given array. C program to find largest element in an array. Recursive C program to linearly search an element in a given . Input and Output.

We've mentioned that Haskell is a purely functional language.

Contact Us

Whereas in imperative languages you usually get things done by giving the computer a series of steps to execute, functional programming is more of defining what stuff is. 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.

So here's how we can recursively determine whether a string is a palindrome. If the first and last letters differ, then declare that the string is not a palindrome. Otherwise, strip off the first and last letters, and determine whether the string that remains—the subproblem—is a palindrome.

One thing I’ve noticed in hunting for a job recently is the number of companies that insist that you write them a code sample to spec. Not just any code sample, but a fully functional, complete application.

This is absurd, for several reasons. Eli White spends a good deal of time arguing why coding tests [ ].

write a recursive method to determine whether a given string is a palindrome
2 Ways to check If String is Palindrome in Java? Recursion and Loop