Java program to reverse the string and check whether it is palindrome or not


Program

package com.candidjava;

import java.util.Scanner;

public class Palindrome
{

	public static void main(String[] args)
	{

		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a string");
		String str = sc.nextLine();

		StringBuffer strone = new StringBuffer(str);
		StringBuffer strtwo = new StringBuffer(strone);

		strone.reverse();

		System.out.println("Orginal String =" + strtwo);
		System.out.println("After Reverse =" + strone);

		if (String.valueOf(strone).compareTo(String.valueOf(strtwo)) == 0)
			System.out.println("Result:Palindrome");
		else
			System.out.println("Result:Not Palindrome");

	}
}

Output

Enter a string

amma

Orginal String =amma

After Reverse =amma

Result:Palindrome





Related Post

Comments


©candidjava.com