Resources » Code Snippets » General

A simple program to check string palindrome in java


Posted Date: 18-Jul-2009  Last Updated:   Category: General    
Author: Member Level: Gold    Points: 5


In this article, you will know a simple program to check string palindrome in java. Learn how to handle a simple program to check string palindrome in java.



A simple program to check string palindrome in java


This code uses a for loop to reverse a string not the reverse() method of Java. Then it compares the reversed string with the actual string using equals() method.


Class Palindrome
{
public static void main(String args[])
{
String s="malayalam";
int i;
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ "is palindrome");
else
System.out.println(s+ "is not a palindrome");
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "A simple program to check string palindrome in java"
Author: palla subramanyam    25 Jan 2012Member Level: Bronze   Points : 2
The more simple and efficient one is

public class Palindrome {
static public String pal(String str, int i, int j){
if(str.length()/2 != i)
if(str.charAt(i)==str.charAt(j))
pal(str,++i,--j);
else
return "String is Not palaindrome";

return "String is palaindrome";
}
public static void main(String[] args) {
String str = args[0];
System.out.println(pal(str,0,str.length()-1));
}
}



Guest Author: vetrivel     04 Aug 2012
very easy to learn


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India