Java Program to check Palindrome
Strings is such a favorite in Java Interviews, that you can expect at least one question on this topic. One of the important concept is Reversing a String, which we have covered earlier.
Logic can be applied using existing knowledge to solve a variety of questions. We'll see the same today.
A Palindrome is a String which if read in reverse order, is still the same. Eg. Radar, kayak etc.
See where we are coming at?
So if you're asked to check if a String is a Palidrome, your knowledge of reversing a String can come in handy!
So, we'll simply reverse a String, and compare if it matches our current String. That's it!
For revision, our Reversal function was:
/** * Reverse a String using for loop * @author computengine.com * @param input * @return */ public String reverseString(String input) { System.out.println("Input String: "+input); //Create a String with no data String output = ""; //Loop through the Input String from the last character and come to the first character. for(int i=input.length()-1;i>=0;i--) { //store each character into the output string. output = output + input.charAt(i); } System.out.println("Reversed String: "+output); return output; }
We'll use the same function to solve the next problem.
If you notice carefully, the revereString function is taking an Input of a String, and Returning Output as a String. That was cleverly written so that it can be reused!
Re-usability is an important concept which you'll find at the workplace. It allows us to make use of a function once written for a variety of purposes. We'll re-use this code now for our next problem.
We had created a separate class for Reversing a String. We don't have to rewrite that code again. We'll simply create an object of the class and call the function for our use.
ReverseString rs = new ReverseString();
Now when we call the revereString function, it will return a String to us. We need to compare that.
Strings have a built in function for comparison.
input.equalsIgnoreCase(rs.reverseString(input)
We're using ignore case, because we don't want our comparison to be case sensitive. If your requirement is to have a case sensitive comparison, you can use input.equals(xyz)
Now, we would want to return a return of the comparison. Here's how we can do that:
// if a string is equal to its reverse, its a palindrome if (input.equalsIgnoreCase(rs.reverseString(input))) return true; else return false;
That's quite compact right?
Here is the complete code for your reference:
package coding; public class CheckPalindrome { public static void main(String[] args) { CheckPalindrome cp = new CheckPalindrome(); System.out.println("Input String is Palindrome: " + cp.checkPalindrome("radar")); } /** * Check if String is palindrome * * @author computengine.com * @param input * @return */ public boolean checkPalindrome(String input) { ReverseString rs = new ReverseString(); // if a string is equal to its reverse, its a palindrome if (input.equalsIgnoreCase(rs.reverseString(input))) return true; else return false; } }
Running a Sample output for a Palindrome String:
Input String: radar
Reversed String: radar
Input String is Palindrome: true
And we can try for a Non Palindrome String:
Comments
Post a Comment