String Palindrome Program in Java
String Palindrome Program in Java
The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and backward (right to left) without changing the meaning of the word or phrase. For example, madam, tattarrattat, noon, civic, racecar, mom, level, etc. In this section, we will create Java programs with different approaches and check the string is palindrome or not.
There are various ways to check whether a string is a palindrome or not. Let’s start with the simple iterative approach using Java-for loop.
Iterative Approach
Filename: PalindromeExample.java
public class PalindromeExample { public static void main(String argvs[]) { String str = "madam"; // string to be check int size = str.length(); // finds the length of the input string boolean isPalindrome = true; // we assume the string is palindrome. for(int i = 0; i < size / 2; i++) { if(str.charAt(i) == str.charAt(size - i - 1)) continue; // characters got matched, moving to next iteration else { // Setting the flag isPalindrome to false as the given string is // not a palindrome. isPalindrome = false; break; // terminate the execution of the loop } } // Displaying the outcome if(isPalindrome) System.out.println("The string " + str + " is a palindrome."); else System.out.println("The string " + str + " is not a palindrome."); } }
Output:
The string madam is a palindrome.
Explanation: First, we compare the first character with the last character, the second character with the second last character, the third character with the third last character and so on, of the input string. If we found a mismatch anywhere while making the comparison, we can say that the given string is not a palindrome. Otherwise, it is a palindrome.
Two-Pointer Approach
Filename: PalindromeExample1.java
public class PalindromeExample1 { public static void main(String argvs[]) { String str = "madam"; // Input string int size = str.length(); // Calculating the length of the input string int ptr1 = 0, ptr2 = size - 1; // two pointers boolean isPalindrome = true; // we assuming that the string is palindrome while(ptr1 < ptr2) { if(str.charAt(ptr1) == str.charAt(ptr2)) { ptr1++; // moving ptr1 one step forward ptr2--; // moving ptr2 one step backward } else { // Setting the flag isPalindrome to false as the given string is // not a palindrome. isPalindrome = false; break; // Coming out of the loop } } // Displaying the outcome if(isPalindrome) System.out.println("The string " + str + " is a palindrome."); else System.out.println("The string " + str + " is not a palindrome."); } }
Output:
The string madam is a palindrome.
Explanation: In the above program, we have taken two pointers, namely, ptr1 and ptr2. The first pointer, ptr1, points to the first letter of the input string and the second pointer, ptr2, points to the last character of the input string. Now, we start the comparison of the characters pointed by those two pointers. If at any place a mismatch is found, the loop terminates and prints string is not palindrome. Else, the input string is a palindrome.
Built-in Method Approach
Filename: PalindromeExample2.java
public class PalindromeExample2 { public static void main(String argvs[]) { String str = "madam"; // Input string String cpyStr = str; // Creates a copy of the given string StringBuilder input = new StringBuilder(); // Creating an object of StringBuilder input.append(str); // Appending the input string input.reverse(); // Reversing the input string // Comparing the modified string with its copy and displaying the result if(cpyStr.equals(input.toString())) System.out.println("The string " + str + " is a palindrome."); else System.out.println("The string " + str + " is not a palindrome."); } }
Output:
The string madam is a palindrome.
Explanation: Since the Java String class does not contain the reverse() method, Therefore, we took the help of the Java StringBuilder class. Using the reverse() method of the Java StringBuilder class, we reverse the input string. Finally, we make a comparison between the copy of the original string and its reversed form and displaying the result accordingly.
Recursive Approach
Filename: PalindromeExample3.java
public class PalindromeExample3 { // Method for finding whether the input string is a palindrome or not static boolean isPalindrome(String input, int start, int end) { // Handling base case if(start >= end) return true; if(input.charAt(start) == input.charAt(end)) // characters matched! Hence, recursively invoking the method return isPalindrome(input, start+1, end - 1); // Mismatch found! Hence, return false. return false; } public static void main(String argvs[]) { String str = "maple"; // Input string int size = str.length(); // Calculating size of the input string // Invoking boolean method isPalindrome() and displaying the result // accordingly if(isPalindrome(str, 0, size - 1)) { System.out.println("The string " + str + " is a palindrome."); } else { System.out.println("The string " + str + " is not a palindrome."); } } }
Output:
The string maple is not a palindrome.
Explanation: We have used the concept of two pointer approach here. The start and end arguments of the isPalindrome() method point to the first and last character of the given string. If we find a mismatch, we come out from the recursion and return false. If the characters, pointed by the first pointer and the second pointer, match, we move further to explore the next set of characters, and we continue to do so until we reach the base case or find a mismatch.