Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 39: Write a Java program to check if a string is a palindrome or not.

Palindrome.java

import java.util.Scanner;

public class Palindrome
{
	//Method to check if a string is Palindrome or not.
	public static boolean checkPalindrome(String str)
	{
		for(int i=0; i < str.length()/2;i++)
		{
			if(str.charAt(i)!=str.charAt(str.length()-i-1))
			{
				return false;
			}
		}
	    return true;
	}
	
	//Main Method
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.println("Enter the String");
		String str = s.nextLine();
		boolean b = Palindrome.checkPalindrome(str);

		if(b == true)
		{
			System.out.println("String is a Palindrome");
		}
		else
		{
			System.out.println("String is not a Palindrome");
		}
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.