Written by Sai Sameera Thota on April 20, 2013 in Java > Programming

Java Program to Check String is Palindrome or Not


Get our Latest updates delivered to your mailbox!

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

import java.io.*;
class palindrome 
	public static void main(String[] args) throws IOException
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		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

{ 1 comment… read it below or add one }

Saifuddin sk April 26, 2017 at 8:06 pm

Thanks…..For the programme simplicity…..


Leave a Comment

Previous post:

Next post: