A simple program to check string palindrome in java


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");
}
}


Comments

Author: palla subramanyam25 Jan 2012 Member 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: vetrivel04 Aug 2012

very easy to learn

Author: kritika17 Aug 2014 Member Level: Bronze   Points : 0

I need a simple program to check whether a string or number is a palindrome or not.without using scanner.



  • 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:
    Email: