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

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

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

27 thoughts on “Java program that checks whether the given string is palindrome or not

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

  2. 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

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

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

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

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

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

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

  9. public class String2 {

    public static void main(String[] args) {
    java.util.Scanner scn=new java.util.Scanner(System.in);
    System.out.println(“enter a name”);
    String s4=scn.nextLine();

    String s2=””;

    int m=s4.length();
    System.out.println(m);
    for(int i=m-1;i>=0;i–)
    {
    s2=s2+s4.charAt(i);

    }
    System.out.println(“reverse string=”+s2);

    boolean x=s4.equalsIgnoreCase(s2);
    if (x==true)
    {
    System.out.println(“Palindrome”);

    }
    else
    System.out.println(“its not a Palindrome”);

    }
    }

  10. public class Palindrome {

    public static void main(String[] args) {
    StringBuffer string = new StringBuffer(“malayalam”);
    if (string.substring(0).equals(string.reverse().toString()))
    System.out.println(“String is palindrom”);
    else
    System.out.println(“String is NOT palindrom”);

    }
    }

  11. public class Palindrome {

    public static void main(String[] args) {
    StringBuffer string = new StringBuffer(“malayalam”);
    if (string.substring(0).equals(string.reverse().toString()))
    System.out.println(“String is palindrom”);
    else
    System.out.println(“String is NOT palindrom”);

    }
    }

  12. public class Palindrome {

    public static void main(String[] args) {
    StringBuffer string = new StringBuffer(“malayalam”);
    if (string.substring(0).equals(string.reverse().toString()))
    System.out.println(“String is palindrom”);
    else
    System.out.println(“String is NOT palindrom”);

    }
    }

  13. Another Simple one without importing any API’s,

    public class FindPalindromeOrNot {

    public static void main(String[] ar) {

    String inputString = “liril”;

    boolean isPalindrome = true;
    char[] inputStringArr = inputString.toCharArray();
    int length = inputStringArr.length;
    int temp = length;
    for (int n = 0; n < length; n++) {
    temp -= 1;
    if (inputStringArr[n] != inputStringArr[temp]) {
    isPalindrome = false;
    }

    }

    System.out.println("Is Palindrome = " + isPalindrome);

    }
    }

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in