Java Program to Check the String is Palindrome

Write a Java Program to Check the given String is Palindrome or not with an example. Any string is a Palindrome in java if the given string 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 string. Next, we used the equalsIgnoreCase() (or use equals()) in If Else statement to compare normStr with revStr. If True, normStr is a palindrome string; otherwise, it is not a palindrome.

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 a Palindrome");
		}		
	}
}
Java Program to Check the String is Palindrome 1

The Java StringBuffer also has a reverse function to apply to the normStr.

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 String to check Palindrome =  ");
		normStr = sc.nextLine();
		
		StringBuffer sbuff = new StringBuffer(normStr);
		
		String revStr = sbuff.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 a Palindrome");
		}		
	}
}

Java palindrome string using StringBuffer output

Please Enter String to check Palindrome =  javaj

The result of String Reverse =  javaj

The Given String javaj is a Palindrome

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

import java.util.Scanner;

public class PalindromeString3 {
	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 Palindrome =  ");
		normStr = sc.nextLine();
		
		String revStr = "";
		
		for(i = normStr.length() - 1; i >= 0; i--) {
			revStr += normStr.charAt(i);
		}
		
		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 a Palindrome");
		}		
	}
}

Java palindrome string output

Please Enter String to check Palindrome =  hellh

The result of String Reverse =  hellh

The Given String hellh is a Palindrome

In this Java palindrome string program, we converted the normStrr string 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 PalindromeString4 {
	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 String to check Palindrome =  ");
		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 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 a Palindrome");
		}		
	}
}

Java palindrome string output

Please Enter String to check Palindrome =  aaabbaaa

The result of String 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 PalindromeString5 {
	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 Palindrome =  ");
		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 String " + normStr + " is not a Palindrome");
		}		
	}
}

Java palindrome string output

Please Enter String to check Palindrome =  huh

The Given String huh is a Palindrome

It is another example of a Java palindrome string.

import java.util.Scanner;

public class PalindromeString6 {
	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 Palindrome =  ");
		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 String " + normStr + " is a Palindrome");
		}
		else {
			System.out.println("\nThe Given String " + normStr + " is not a Palindrome");
		}		
	}
}
Please Enter String to check Palindrome =  hopoh

The Given String hopoh is a Palindrome

Java Program to Check the String is Palindrome using Recursion.

import java.util.Scanner;

public class PalindromeString7 {
	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 Palindrome =  ");
		normStr = sc.nextLine();
		
		String revStr = reverseString(normStr);
		System.out.println("\nThe result of String 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 String " + normStr + " is a Palindrome");
		}
		else {
			System.out.println("\nThe Given String " + normStr + " is not a Palindrome");
		}		
	}
	private static String reverseString(String normStr) {
		if(normStr.isEmpty()) {
			return normStr;
		}
		return reverseString(normStr.substring(1)) + normStr.charAt(0);
	}
}

Java recursive palindrome string output

Please Enter String to check Palindrome =  hello

The result of String Reverse =  olleh

The Given String hello is not a Palindrome