Write a program How to check Palindrome String


The program defines two classes, Palindrome_String and Palindrome. Palindrome_String contains the main() method, while Palindrome contains the check_Palindrome() method which checks if a given string is a palindrome.

Inside the main() method, it calls the check_Palindrome() method of the Palindrome class with two different strings - "java" and "madam" and outputs the result to the console using the System.out.println() method.

The check_Palindrome() method of the Palindrome class takes a string str as input, creates a new StringBuilder object s with the string str, and reverses it using the reverse() method. It then converts the reversed StringBuilder object back to a string using the toString() method and stores it in the rev variable.

Finally, it checks if str is equal to rev. If they are equal, it returns true, indicating that the string is a palindrome. Otherwise, it returns false, indicating that the string is not a palindrome.

Overall, this program takes a string as input and checks whether it is a palindrome or not using the check_Palindrome() method of the Palindrome class.

Source Code

class Palindrome_String
{
	public static void main(String[] args)
	{   
		System.out.println(Palindrome.check_Palindrome("java"));
		System.out.println(Palindrome.check_Palindrome("madam"));
	}  
}  
public class Palindrome
{
	public static boolean check_Palindrome(String str)
	{  
		StringBuilder s = new StringBuilder(str);  
		s.reverse();  
		String rev = s.toString();  
		if(str.equals(rev))
		{
			return true;  
		}
		else
		{
			return false;  
		}
	}
}

Output

false
true

Example Programs