In this post, we’ll look at how to write a Java programme that checks whether a given string is a palindrome. A palindrome string is a string in which the reverse of a string is the same as the original string. A palindrome string is a string that remains the same whether read from left to right or right to left. The palindrome string can be read from both ends. When written backwards, palindrome strings remain unchanged.
Palindrome String
A palindrome is a string that is equal to the reverse of the same string. A palindrome is a string that remains unchanged when read in the opposite direction. Palindrome strings have vertical axis symmetry. The string will remain the same whether read from left to right or right to left. The first and last characters are the same, as are the second and second-to-last, third and third-to-last, and so on.
Palindrome strings are those listed below that remain the same when written in the opposite order.
MADAM, MALAYALAM
The strings in the list below are not palindromes. A different string will appear on the reverse.
BOOK, TEXT
The logic for determining the palindrome string is as follows. Get the given string and calculate its length. In a loop, iterate through the entire string. Read the string character by character and prefix with a local variable. Following the completion of the loop, the local variable will contain the reverse string of a given string. Compare the given string to the local variable. If both strings are the same, the given string is a palindrome. Otherwise, the provided string is not a palindrome.
In the Java example below, the programme determines whether or not the given string is a palindrome.
package in.yawin; public class PalindromeString { public static void main(String[] args) { String str = "MALAYALAM"; String tempStr = ""; for (int i = 0; i < str.length(); i++) { tempStr = str.charAt(i) + tempStr; } if (str.equalsIgnoreCase(tempStr)) { System.out.println(str + " is a Palindrome string."); } else { System.out.println(str + " is not a Palindrome string."); } } }
Output
MALAYALAM is a Palindrome string.
Explanation
The java programme will check to see if the given string is a palindrome. The provided string is saved in the variable “str.” Another local variable is created to store the inverse of the given string. tempStr is the name of the variable. A for loop iterates through the given string character by character. The programme will read a character and prefix it with the tempStr variable during each loop execution. Following the for loop’s execution, the tempStr variable contains the inverse of the given string. The if statement compares the given string to the reverse string. If both strings are the same, the given string is a palindrome. Otherwise, the provided string is not a palindrome.
Java program to check palindrome using recursion
The recursive method is used in the following example to find the palindrome string. As input, the recursive method will take a string and an index integer value. The starting index value is zero. When the recursive method is called, the index value is increased.
The initial value of the recursive method is zero. The programme will examine the first and last characters. If both are matched, the index value is increased and the call is repeated. If the value is not found, it will return false. The second recursive call will compare the second and last second character. If it is a match, the index will be increased and moved forward. The java programme will return true if the index value is increased by half the length of the string.
package in.yawin; public class PalindromeStringRecursive { public static void main(String[] args) { String str = "MALAYALAM"; if (palindrome(str, 0)) { System.out.println(str + " is a Palindrome string."); } else { System.out.println(str + " is not a Palindrome string."); } } public static boolean palindrome(String str, int n) { if (n > str.length() - n) { return true; } if (str.charAt(n) != str.charAt(str.length() - 1 - n)) { return false; } return palindrome(str, n + 1); } }
Output
MALAYALAM is a Palindrome string.