Java Program to Check String is Palindrome or Not

We have already written Palindrome Program in C, A string is taken as an input to the program. The length of the string is calculated using the string method length() and is stored in the variable n. An empty string variable rev is declared and the last character of the given string is appended to the rev variable using the string method charAt(). In this process, the reverse of the given string is stored in the rev variable. The reversed string is compared with the given string using the string method equals(). If the reversed string equals the given string then the given string can be considered as a palindrome otherwise it is not a palindrome.

For example, let us consider a string.
String str = “programmingunit”;
char res = str.chatAt(4);
charAt() returns the character that is present at the given index.
int len = str.length();
Length method returns the length of the given string
boolean ret = str.equals(“sam”);
The equals method returns true if the given string equals the argument else it returns false.

Java Palindrome Program

class palindrome 
	public static void main(String[] args) throws IOException
		BufferedReader br = new BufferedReader(new InputStreamReader(;
		System.out.print("\nEnter a String : ");
		String str = br.readLine();
		String rev = "";
		int n = str.length();
		for(int i=n-1 ; i>=0 ; i--)
			rev = rev + str.charAt(i);
			System.out.println("\nGiven string is a palindrome");
			System.out.println("\nGiven string is not a palindrome");

Download Source Code

Java Palindrome Program

Leave a Reply