Write a Java Program to Check whether the String is Palindrome or not with an example. Any text is a Palindrome string if it is exactly equal to that string’s reverse.
In this example, we are using the StringBuilder reverse function to reverse the normStr and assign it to the revStr. Next, we used the equalsIgnoreCase() (or use equals()) in the If Else statement to compare normStr with revStr. If True, normStr is a palindrome string; otherwise, it is not.
import java.util.Scanner; public class PalindromeString1 { private static Scanner sc; public static void main(String[] args) { String normStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter String to check Palindrome = "); normStr = sc.nextLine(); StringBuilder sb = new StringBuilder(normStr); String revStr = sb.reverse().toString(); System.out.println("\nThe result of String Reverse = " + revStr); if(normStr.equalsIgnoreCase(revStr) == true) { System.out.println("\nThe Given String " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given String " + normStr + " is not"); } } }

Java Program to Check the String is Palindrome using StringBuffer and reverse function
The StringBuffer also has a reverse function to apply to the normStr. We use this function to check the palindrome string.
import java.util.Scanner; public class PalindromeString2 { private static Scanner sc; public static void main(String[] args) { String normStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); normStr = sc.nextLine(); StringBuffer sbuff = new StringBuffer(normStr); String revStr = sbuff.reverse().toString(); System.out.println("\nThe result of Reverse = " + revStr); if(normStr.equalsIgnoreCase(revStr) == true) { System.out.println("\nThe Given " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given " + normStr + " is not"); } } }
Using Java StringBuffer output.

Java Program to Check the String is Palindrome using for loop
In this program, we used for loop to iterate normStr from end to start (in reverse order) and concerted it to revStr. Next, we used (normStr.equalsIgnoreCase(revStr) == true) to check the palindrome string.
import java.util.Scanner; public class PaliStr3 { private static Scanner sc; public static void main(String[] args) { String normStr; int i; sc= new Scanner(System.in); System.out.print("\nPlease Enter String to check = "); normStr = sc.nextLine(); String revStr = ""; for(i = normStr.length() - 1; i >= 0; i--) { revStr += normStr.charAt(i); } System.out.println("\nThe result of Reverse = " + revStr); if(normStr.equalsIgnoreCase(revStr) == true) { System.out.println("\nThe Given String " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given " + normStr + " is not"); } } }

In this palindrome string program, we converted the normStrr to the StrCharArr character array. Next, we used for loop to iterateStrCharArr from right to left, assigned the last character to the first index, etc. Then, we used equalsIgnoreCase to compare and check for palindrome string.
import java.util.Scanner; public class PaliStr4 { private static Scanner sc; public static void main(String[] args) { String normStr; int i, j = 0; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text to check = "); normStr = sc.nextLine(); char[] StrCharArr = normStr.toCharArray(); for(i = StrCharArr.length - 1; i >= 0; i--) { StrCharArr[j++] = normStr.charAt(i); } String revStr = String.valueOf(StrCharArr); System.out.println("\nThe result of Reverse = " + revStr); if(normStr.equalsIgnoreCase(revStr) == true) { System.out.println("\nThe Given String " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given " + normStr + " is not"); } } }
Please Enter Text to check = aaabbaaa
The result of Reverse = aaabbaaa
The Given String aaabbaaa is a Palindrome
In this palindrome string program, we compare the first character with the last character, and so on. If they are not equal, the flag will be 1, followed by Break Statement to exit from the loop. If the flag value equals 0, the string is a palindrome.
import java.util.Scanner; public class PaliStr5 { private static Scanner sc; public static void main(String[] args) { String normStr; int i, flag = 0; sc= new Scanner(System.in); System.out.print("\nPlease Enter String to check = "); normStr = sc.nextLine(); char[] StrCharArr = normStr.toCharArray(); for(i = 0; i < StrCharArr.length; i++) { if(StrCharArr[i] != StrCharArr[StrCharArr.length - i -1]) { flag = 1; break; } } if(flag == 0) { System.out.println("\nThe Given String " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given " + normStr + " is not"); } } }
Please Enter String to check = huh
The Given String huh is a Palindrome
It is another example of a palindrome string. Here, instead of using the flag, we simply used the charcater index and break statements.
import java.util.Scanner; public class PaliStr6 { private static Scanner sc; public static void main(String[] args) { String normStr; int i; sc= new Scanner(System.in); System.out.print("\nPlease Enter text = "); normStr = sc.nextLine(); int len = normStr.length(); int endIndex = len - 1; for(i = 0; i <= endIndex; i++) { if(normStr.charAt(i) != normStr.charAt(endIndex)) { break; } endIndex--; } if(i >= endIndex) { System.out.println("\nThe Given " + normStr + " is a Palindrome"); } else { System.out.println("\nThe Given " + normStr + " is not"); } } }
Please Enter text = hopoh
The Given hopoh is a Palindrome
Java Program to Check the String is Palindrome using Recursion
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { String normStr; sc= new Scanner(System.in); System.out.print("Please Enter text = "); normStr = sc.nextLine(); String revStr = reverseString(normStr); System.out.println("\nThe result of Reverse = " + revStr); if(normStr.equalsIgnoreCase(revStr) == true) { System.out.println("The Given " + normStr + " is a Palindrome"); } else { System.out.println("The Given " + normStr + " is not"); } } private static String reverseString(String normStr) { if(normStr.isEmpty()) { return normStr; } return reverseString(normStr.substring(1)) + normStr.charAt(0); } }
