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

Friday, October 2nd, 2009

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

Author Name :
Ranjith

Total : 16 Comments


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

  1. Rishi says:

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

  2. Palla Subramanyam says:

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

  3. Aaliyaa says:

    Watevr Bullshits….nthng appropriate…jst a waste of tym….losrs!!!!!!!!

  4. Surya says:

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

  5. K.SIVA KUMAR says:

    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

  6. harsha says:

    all sucking program nothing will compile

  7. unknown says:

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

  8. luck says:

    nee amma pukula sulli
    okkati complie kadhu

  9. anjaneyulu says:

    excelent

  10. Bibhuti mallick says:

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

  11. janine dragneel says:

    this is helpful .. thanks guys.. God bless !!

  12. mujeeb says:

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

  13. Pulkit says:

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

  14. Pulkit says:

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

  15. Isha says:

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

  16. Tejas says:

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

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: