program to check palindrome in java

Learn Java Programming for Beginners

Palindrome is sequence of numbers and characters which reads same in its forward and back word order.for example EYE,MADAM etc .In this Program we use a predefined string ,charAt method is used to get characters from string first character and last character is compared using if statement if condition is false during for loop then string is not palindrome .

program to check palindrome in java

class palindrome
{
public static void main(String args[ ])
{
String str_palindrome = new String("MALAYALAM");
int i,j,len = str_palindrome.length();
boolean flag=true;

System.out.println("String :"+str);
for(i=0,j=len-1;i<=len/2;i++,j--)
{
if(str_palindrome.charAt(i)==str_palindrome.charAt(j))
flag=true;
else
{
flag=false;
break;
}
}

if(flag==true)
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}

}

output:

c:\>javac palindrome.java

c:\>java palindrome

String : MALAYALAM

String Is palindrome

program to check palindrome in java

program to check palindrome in java

You may also like...

2 Responses

  1. Shreya says:

    really nice palindrome program

Leave a Reply

Your email address will not be published. Required fields are marked *