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


Program

//Program to check given String is Palindrome
public class PalindromeChecking
{
public static void main(String[] args)
{
String inpstr ="AMMA"; //Input String
char[] inpArray = inpstr.toCharArray(); //Converting String to Character Array
char[] revArray = new char[inpArray.length]; //Arrayvariable to store reversed string
int j=0;
for (int i = inpArray.length - 1; i >= 0; i--) //To Reverse the String
{
revArray[j]=inpArray[i];
j++;
}
String revstr=String.valueOf(revArray); //Converting Character Array to String
if(inpstr.equals(revstr)) //Compare both strings
{
System.out.println("The given string is a Palindrome");
}
else
{
System.out.println("The given string is not a Palindrome");
}

}
}

Output

The given string is a Palindrome





Related Post

Comments


©candidjava.com