In this post we will learn how to check whether a given String/Number is a Palindrome or not in java.
Before start writing a java program let’s try to understand.
What is Palindrome?
If a String or a Number is a palindrome if it remains unchanged when reversed, for example “mom” is a palindrome as reverse of the mom is again mom. Another example is “malayalam” or 12344321.
You may refer Reverse a string in Java program with 5 Different Ways that may be helpful to you to understand the logic used behind the Palindrome check program
Approach 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials Java program to Check Whether a Given String/Number * is a Palindrome or not */ public class ClientTest1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter string which you want to check whether Palindrome or not::"); String inputText = scanner.next(); checkInputTextPalindrome(inputText); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void checkInputTextPalindrome(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()){ System.out.println("Enter valid input text.."); return; } boolean isPalidrome =false; String reversedOutput = reverseInput(inputText); if(inputText.equalsIgnoreCase(reversedOutput)) isPalidrome = true; if(isPalidrome) System.out.println("Input String is Palidrome."); else System.out.println("Input String is not palidrome."); } private static String reverseInput(String inputText) { String reverse = ""; for (int i = inputText.length()-1; i>=0 ; i--) { char c = inputText.charAt(i); reverse = reverse+c; } return reverse; } } |
Approach 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials Java program to Check Whether a Given String/Number * is a Palindrome or not */ public class ClientTest2 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter string which you want to check whether Palindrome or not::"); String inputText = scanner.next(); checkInputTextPalindrome(inputText); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void checkInputTextPalindrome(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()){ System.out.println("Enter valid input text.."); return; } boolean isPalidrome =false; String reversedOutput = reverseInput(inputText); if(inputText.equalsIgnoreCase(reversedOutput)) isPalidrome = true; if(isPalidrome) System.out.println("Input String is Palidrome."); else System.out.println("Input String is not palidrome."); } private static String reverseInput(String inputText) { String reverse = ""; //Convert string into character array char[] charArray = inputText.toCharArray(); for (int i = inputText.length()-1; i>=0 ; i--) { reverse +=charArray[i]; } return reverse; } } |
Approach 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials Java program to Check Whether a Given String/Number * is a Palindrome or not */ public class ClientTest3 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out .println("Enter string which you want to check whether Palindrome or not::"); String inputText = scanner.next(); checkInputTextPalindrome(inputText); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void checkInputTextPalindrome(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text.."); return; } boolean isPalidrome = false; String reversedOutput = reverseInput(inputText); if (inputText.equalsIgnoreCase(reversedOutput)) isPalidrome = true; if (isPalidrome) System.out.println("Input String is Palidrome."); else System.out.println("Input String is not palidrome."); } private static String reverseInput(String inputText) { // Convert string into byte array byte[] bytes = inputText.getBytes(); byte[] revByteArr = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) { revByteArr[i] = bytes[bytes.length - i - 1]; } String reverseString = new String(revByteArr); return reverseString; } } |
Approach 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials Java program to Check Whether a Given String/Number * is a Palindrome or not */ public class ClientTest4 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out .println("Enter string which you want to check whether Palindrome or not::"); String inputText = scanner.next(); checkInputTextPalindrome(inputText); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void checkInputTextPalindrome(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text.."); return; } boolean isPalidrome = false; String reversedOutput = reverseInput(inputText); if (inputText.equalsIgnoreCase(reversedOutput)) isPalidrome = true; if (isPalidrome) System.out.println("Input String is Palidrome."); else System.out.println("Input String is not palidrome."); } private static String reverseInput(String inputText) { // Convert string into character array char[] inputByteArray = inputText.toCharArray(); int left, right = inputByteArray.length - 1; for (left = 0; left < right; left++, right--) { // Swap values of left and right index char temp = inputByteArray[left]; inputByteArray[left] = inputByteArray[right]; inputByteArray[right] = temp; } String reverseString = new String(inputByteArray); return reverseString.toString(); } } |
Approach 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials Java program to Check Whether a Given String/Number * is a Palindrome or not */ public class ClientTest1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter string which you want to check whether Palindrome or not::"); String inputText = scanner.next(); checkInputTextPalindrome(inputText); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void checkInputTextPalindrome(String inputText) { boolean isPalidrome =false; // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()){ System.out.println("Enter valid input text.."); return; } /* * Here I have used StringBuilder class if you required thread safety then * you may use StringBuffer class which has also reverse method */ StringBuilder sb = new StringBuilder(inputText); sb.reverse(); String reversedOutput = sb.toString(); //Above action may be perform in single line,Just to make you understand i have splitted into three lines. //String reversedOutput = new StringBuilder(inputText).reverse().toString(); if(inputText.equalsIgnoreCase(reversedOutput)) isPalidrome = true; if(isPalidrome) System.out.println("Input String is Palidrome."); else System.out.println("Input String is not palidrome."); } } |
I have checked all the above programs for few inputs as below. You may check for other input as well
1 2 3 4 5 6 7 8 9 10 11 |
Enter string which you want to check whether Palindrome or not:: malayalam Input String is Palidrome. Enter string which you want to check whether Palindrome or not:: 12344321 Input String is Palidrome. Enter string which you want to check whether Palindrome or not:: KKJavaTutorials Input String is not palidrome. |
You would love to watch me on YouTube:
That’s all about this topic How to check Whether a Given String/Number is a Palindrome in java.
You May Also Like:
Reverse a string in Java (6 Different Ways)
Splitting a String in Java using a delimiter
Count number of words in a string java
Count total number of times each character appears in the string in java
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
Java Program to find the frequency of each character in String ?
If you have any feedback or suggestion please feel free to drop in below comment box.