Java Program to Check the String is Palindrome

Write a Java Program to Check the String is Palindrome or not with an example. Any text is a Palindrome string in java if it is exactly equal to that string’s reverse.

In this Java Palindrome string example, we are using the StringBuilder reverse function to reverse the normStr and assigned it to the revStr. Next, we used the equalsIgnoreCase() (or use equals()) in If Else statement to compare normStr with revStr. If True, normStr is a palindrome string in Java; 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 1

The Java StringBuffer also has a reverse function to apply to the normStr. We use this function to check the palindrome string in java.

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 StringBuffer output

Please Enter Text =  javaj

The result of Reverse =  javaj

The Given javaj is a Palindrome

In this Java palindrome string 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 in java.

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");
		}		
	}
}
Please Enter String to check =  hellh

The result of Reverse =  hellh

The Given String hellh is a Palindrome

In this Java palindrome string program, we converted the normStrr to the StrCharArr character array. Next, we used for loop to iterateStrCharArr from right to left and 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 Java program, we compare the first character with the last character, and so on. If they are not equal, the flag will be one 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 Java palindrome string.

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 Example7 {
	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 text =  ");
		normStr = sc.nextLine();
		
		String revStr = reverseString(normStr);
		System.out.println("\nThe result of Reverse =  " + revStr);
		
		for(i = 0; i < normStr.length(); i++) {
			if(normStr.charAt(i) != revStr.charAt(i)) {
				flag = 1;
				break;
			}
		}			
		if(flag == 0) {
			System.out.println("\nThe Given " + normStr + " is a Palindrome");
		}
		else {
			System.out.println("\nThe Given " + normStr + " is not");
		}		
	}
	private static String reverseString(String normStr) {
		if(normStr.isEmpty()) {
			return normStr;
		}
		return reverseString(normStr.substring(1)) + normStr.charAt(0);
	}
}
Please Enter text =  hello

The result of Reverse =  olleh

The Given hello is not