Java program that checks whether the given string is palindrome or not

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

class palindrome
{
public static void main(String[] args)
{
StringBuffer s1=new StringBuffer(args[0]);
StringBuffer s2=new StringBuffer(s1);
s1.reverse();
System.out.println(“Given String is:”+s2);
System.out.println(“Reverse String is”+s1);
if(String.valueOf(s1).compareTo(String.valueOf(s2))==0)
System.out.println(“Palindrome”);
else
System.out.println(“Not Palindrome”);
}
}

Output:

Java palindrome madam
Given String is:madam
Reverse String is madam
Palindrome

Java palindrome harish
Given String is:harish
Reverse String is hsirah
Not Palindrome

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

19 Responses to “Java program that checks whether the given string is palindrome or not”

  1. public class palindrome
    {
    public static void main(String args[])
    {
    String s=args[0];
    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”);
    }
    }

    Reply
  2. Palla Subramanyam

    The More simplest 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));
    }
    }

    Reply
  3. import java.util.Scanner;
    public class PalindromeSentence

    {
    public static void main( String[] argv )
    {
    System.out.print( “Enter your phrase: ” );
    String phrase = new Scanner(System.in).nextLine();
    String eval = phrase.toLowerCase();
    boolean isPalindrome = true;
    int first , last;
    for( first=0 , last=eval.length()-1 ; first < last ; first++ , last– )
    {
    while( (int)eval.charAt(first) ‘z’ ) first++;
    while( (int)eval.charAt(last ) ‘z’ ) last–;
    if( first > last || eval.charAt(first) != eval.charAt(last) ) isPalindrome = false;
    }
    System.out.printf( “\”%s\” is%s a palindrome.\n”, phrase, (isPalindrome ? “” : ” not” ) );
    }
    }

    Reply
  4. K.SIVA KUMAR

    class palindrome
    {

    public static void main(String[] args)
    {
    String str = “malayalam”; // write a string name

    StringBuffer sb = new StringBuffer(str).reverse();

    String strRev = sb.toString();

    if(str.equalsIgnoreCase(strRev))
    {
    System.out.println(” given string is Palindrome”);
    }
    else
    {
    System.out.println(” given string is Not a Palindrome”);
    }
    }
    }
    OUTPUT
    GIVEN STRING IS PALINDROME

    Reply
  5. unknown

    Im getting an error in StringBuffer example….
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at Pallindrome.main(Pallindrome.java:10)

    Reply
  6. Bibhuti mallick

    i have written the program but i am facing problem to run it

    Reply
  7. import java.lang.*;
    import java.io.*;
    class palindrome
    {
    public static void main(String[]args) throws IOException
    {
    String str;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println(“enter the string”);
    str=br.readLine();
    StringBuffer sb=new StringBuffer(str);
    String strRev=new String(sb.reverse());
    if(str.equalsIgnoreCase (strRev))
    {
    System.out.println(“it is a palindrome”);
    }
    else
    {
    System.out.println(“it is not a palindrome”);
    }
    }
    }

    Reply
  8. Simplest of all above—–>

    public Palindrome(String first,String second)
    {
    this.f1=first;
    this.f2=second;
    int num1=f1.length();
    int num2=f2.length();
    char a=f1.charAt(0);
    char b=f2.charAt(num2-1);

    if (num1==num2)
    {
    for(int i=0;i=0;j–)
    {char d=f2.charAt(j);
    if(c==d)
    {
    if(i==num1-1)
    {System.out.println(“Palindrome”);
    }

    }

    }

    }
    }
    else
    {System.out.println(“Not Palindrome”);}
    }

    Reply
  9. public Palindrome(String first,String second)
    {
    this.f1=first;
    this.f2=second;
    int num1=f1.length();
    int num2=f2.length();
    char a=f1.charAt(0);
    char b=f2.charAt(num2-1);

    if (num1==num2)
    {
    for(int i=0;i=0;j–)
    {char d=f2.charAt(j);
    if(c==d)
    {
    if(i==num1-1)
    {System.out.println(“Palindrome”);
    }

    }

    }

    }
    }
    else
    {System.out.println(“Not Palindrome”);}
    }

    Reply
  10. class Palindrome
    {
    public void input(String s)
    {
    boolean result=false;
    String reverse=””;
    for(int i=s.length()-1;i>=0;i–)
    {
    char ans=s.charAt(i);
    reverse=reverse+ans;
    result=s.equals(reverse);
    if(result==true)
    System.out.println(s+”is palindrome”);
    else
    System.out.println(s+”is not palindrome”);
    }
    }
    }

    Reply
  11. public class Conversion1 {
    public static void main(String[] args)
    {
    int i;
    String s2=””;
    String s1 = new String(“coffoc”);

    int len_str = s1.length();
    for(i=len_str-1;i>=0;i–)
    {
    s2 = s2+s1.charAt(i);
    }
    System.out.println(“Hence reverse of the string is:”+s2);

    if(String.valueOf(s1).compareTo(String.valueOf(s2))==0)
    System.out.println(“Palindrome”);

    else
    System.out.println(“Not Palindrome”);
    }
    }

    Reply
  12. if the given String is not a palindrome? how will you change it into a palindrome?

    Reply

Leave a Reply