Check string for palindrome












74














A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.



To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.



Here is my code:



public class Aufg1 {
public static void main(String args) {
String wort = "reliefpfpfeiller";
char warray = wort.toCharArray();
System.out.println(istPalindrom(warray));
}

public static boolean istPalindrom(char wort){
boolean palindrom = false;
if(wort.length%2 == 0){
for(int i = 0; i < wort.length/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}else{
for(int i = 0; i < (wort.length-1)/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}
return palindrom;
}
}









share|improve this question




















  • 3




    Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
    – barrowc
    Nov 10 '10 at 1:47
















74














A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.



To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.



Here is my code:



public class Aufg1 {
public static void main(String args) {
String wort = "reliefpfpfeiller";
char warray = wort.toCharArray();
System.out.println(istPalindrom(warray));
}

public static boolean istPalindrom(char wort){
boolean palindrom = false;
if(wort.length%2 == 0){
for(int i = 0; i < wort.length/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}else{
for(int i = 0; i < (wort.length-1)/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}
return palindrom;
}
}









share|improve this question




















  • 3




    Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
    – barrowc
    Nov 10 '10 at 1:47














74












74








74


36





A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.



To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.



Here is my code:



public class Aufg1 {
public static void main(String args) {
String wort = "reliefpfpfeiller";
char warray = wort.toCharArray();
System.out.println(istPalindrom(warray));
}

public static boolean istPalindrom(char wort){
boolean palindrom = false;
if(wort.length%2 == 0){
for(int i = 0; i < wort.length/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}else{
for(int i = 0; i < (wort.length-1)/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}
return palindrom;
}
}









share|improve this question















A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.



To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.



Here is my code:



public class Aufg1 {
public static void main(String args) {
String wort = "reliefpfpfeiller";
char warray = wort.toCharArray();
System.out.println(istPalindrom(warray));
}

public static boolean istPalindrom(char wort){
boolean palindrom = false;
if(wort.length%2 == 0){
for(int i = 0; i < wort.length/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}else{
for(int i = 0; i < (wort.length-1)/2-1; i++){
if(wort[i] != wort[wort.length-i-1]){
return false;
}else{
palindrom = true;
}
}
}
return palindrom;
}
}






java arrays string char palindrome






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 18 '15 at 23:07









Michael Myers

154k37256280




154k37256280










asked Nov 9 '10 at 21:28









Artjom Zabelin

35.3k111311530




35.3k111311530








  • 3




    Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
    – barrowc
    Nov 10 '10 at 1:47














  • 3




    Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
    – barrowc
    Nov 10 '10 at 1:47








3




3




Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
– barrowc
Nov 10 '10 at 1:47




Not sure if this is intentional but the string in your example - reliefpfpfeiller - isn't a palindrome
– barrowc
Nov 10 '10 at 1:47












34 Answers
34






active

oldest

votes













1 2
next












164














Why not just:



public static boolean istPalindrom(char word){
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}


Example:



Input is "andna".

i1 will be 0 and i2 will be 4.



First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).

So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).

Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.






share|improve this answer



















  • 1




    instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
    – user0946076422
    Aug 15 '15 at 3:44










  • @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
    – Vijay Tholpadi
    Mar 10 '17 at 3:11






  • 2




    @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
    – dcp
    Mar 15 '17 at 12:44



















111














You can check if a string is a palindrome by comparing it to the reverse of itself:



public static boolean isPalindrome(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}


or for versions of Java earlier than 1.5,



public static boolean isPalindrome(String str) {
return str.equals(new StringBuffer().append(str).reverse().toString());
}


EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!






share|improve this answer



















  • 10




    Compare the complexity of your algorithm with respect to others.
    – Fernando Pelliccioni
    Feb 3 '14 at 14:24






  • 2




    @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
    – aioobe
    Aug 23 '15 at 8:16






  • 1




    @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
    – aioobe
    Aug 26 '15 at 21:24






  • 15




    Here my analysis: componentsprogramming.com/palindromes
    – Fernando Pelliccioni
    Jun 6 '16 at 21:09






  • 1




    @FernandoPelliccioni nice analysis
    – Saravana
    Jul 12 '16 at 4:11



















54














A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:



boolean isPalindrome(String str) {    
int n = str.length();
for( int i = 0; i < n/2; i++ )
if (str.charAt(i) != str.charAt(n-i-1)) return false;
return true;
}





share|improve this answer































    14














    Alternatively, recursion.



    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:



    private boolean isPalindrome(String s) {
    int length = s.length();

    if (length < 2) // If the string only has 1 char or is empty
    return true;
    else {
    // Check opposite ends of the string for equality
    if (s.charAt(0) != s.charAt(length - 1))
    return false;
    // Function call for string with the two ends snipped off
    else
    return isPalindrome(s.substring(1, length - 1));
    }
    }




    OR even shorter, if you'd like:



    private boolean isPalindrome(String s) {
    int length = s.length();
    if (length < 2) return true;
    else return s.charAt(0) != s.charAt(length - 1) ? false :
    isPalindrome(s.substring(1, length - 1));
    }





    share|improve this answer



















    • 2




      Nice code, recursion makes it really easy and less lines on code.
      – Akash5288
      Jan 31 '17 at 6:35










    • the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
      – vault
      Apr 19 at 23:05



















    8














    Go, Java:



    public boolean isPalindrome (String word) {
    String myWord = word.replaceAll("\s+","");
    String reverse = new StringBuffer(myWord).reverse().toString();
    return reverse.equalsIgnoreCase(myWord);
    }

    isPalindrome("Never Odd or Even"); // True
    isPalindrome("Never Odd or Even1"); // False





    share|improve this answer





























      4














      public class Aufg1 {
      public static void main(String args) {
      String wort = "reliefpfpfeiller";
      char warray = wort.toCharArray();
      System.out.println(istPalindrom(warray));
      }

      public static boolean istPalindrom(char wort){
      if(wort.length%2 == 0){
      for(int i = 0; i < wort.length/2-1; i++){
      if(wort[i] != wort[wort.length-i-1]){
      return false;
      }
      }
      }else{
      for(int i = 0; i < (wort.length-1)/2-1; i++){
      if(wort[i] != wort[wort.length-i-1]){
      return false;
      }
      }
      }
      return true;
      }
      }





      share|improve this answer



















      • 2




        simplified a bit. but I like dcp's answer!
        – Casey
        Nov 9 '10 at 21:36



















      4














      also a different looking solution:



      public static boolean isPalindrome(String s) {

      for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {

      if ( s.charAt(i) != s.charAt(j) ) {
      return false;
      }
      }

      return true;
      }





      share|improve this answer





























        4














        And here a complete Java 8 streaming solution. An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done.



        public static void main(String args) {
        for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
        System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
        }
        }

        public static boolean isPalindrome(String str) {
        return IntStream.range(0, str.length() / 2)
        .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
        }


        Output is:



        testing testset is palindrome=true
        testing none is palindrome=false
        testing andna is palindrome=true
        testing haah is palindrome=true
        testing habh is palindrome=false
        testing haaah is palindrome=true





        share|improve this answer





















        • Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
          – gil.fernandes
          Apr 5 at 9:47



















        3














        Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.



        public int isPalindrome(String a) {
        //Remove all spaces and non alpha characters
        String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
        //System.out.println(ab);

        for (int i=0; i<ab.length()/2; i++) {
        if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
        return 0;
        }
        }
        return 1;
        }





        share|improve this answer





























          2














          public class palindrome {
          public static void main(String args) {
          StringBuffer strBuf1 = new StringBuffer("malayalam");
          StringBuffer strBuf2 = new StringBuffer("malayalam");
          strBuf2.reverse();


          System.out.println(strBuf2);
          System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
          if ((strBuf1.toString()).equals(strBuf2.toString()))
          System.out.println("palindrome");
          else
          System.out.println("not a palindrome");
          }


          }






          share|improve this answer





























            2














            I worked on a solution for a question that was marked as duplicate of this one.
            Might as well throw it here...



            The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.



            Here's the ugly solution with a small test class:



            public class Palindrome {
            public static boolean isPalendrome(String arg) {
            return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
            }
            public static void main(String args) {
            System.out.println(isPalendrome("hiya"));
            System.out.println(isPalendrome("star buttons not tub rats"));
            System.out.println(isPalendrome("stab nail at ill Italian bats!"));
            return;
            }
            }


            Sorry that it is kind of nasty - but the other question specified a one-liner.






            share|improve this answer



















            • 1




              Just curious, why the ternary operator at the end?
              – typingduck
              Apr 17 '17 at 16:24










            • Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
              – Marc
              May 24 '17 at 15:51



















            2














            Amazing how many different solutions to such a simple problem exist! Here's another one.



            private static boolean palindrome(String s){
            String revS = "";
            String checkS = s.toLowerCase();
            String checkSArr = checkS.split("");

            for(String e : checkSArr){
            revS = e + revS;
            }

            return (checkS.equals(revS)) ? true : false;
            }





            share|improve this answer





























              1














              Try this out :



              import java.util.*;
              public class str {

              public static void main(String args)
              {
              Scanner in=new Scanner(System.in);
              System.out.println("ENTER YOUR STRING: ");
              String a=in.nextLine();
              System.out.println("GIVEN STRING IS: "+a);
              StringBuffer str=new StringBuffer(a);
              StringBuffer str2=new StringBuffer(str.reverse());
              String s2=new String(str2);
              System.out.println("THE REVERSED STRING IS: "+str2);
              if(a.equals(s2))
              System.out.println("ITS A PALINDROME");
              else
              System.out.println("ITS NOT A PALINDROME");
              }
              }





              share|improve this answer































                1














                I'm new to java and I'm taking up your question as a challenge to improve my knowledge.



                import java.util.ArrayList;
                import java.util.List;

                public class PalindromeRecursiveBoolean {

                public static boolean isPalindrome(String str) {

                str = str.toUpperCase();
                char strChars = str.toCharArray();

                List<Character> word = new ArrayList<>();
                for (char c : strChars) {
                word.add(c);
                }

                while (true) {
                if ((word.size() == 1) || (word.size() == 0)) {
                return true;
                }
                if (word.get(0) == word.get(word.size() - 1)) {
                word.remove(0);
                word.remove(word.size() - 1);
                } else {
                return false;

                }

                }
                }
                }



                1. If the string is made of no letters or just one letter, it is a
                  palindrome.

                2. Otherwise, compare the first and last letters of the string.

                  • If the first and last letters differ, then the string is not a palindrome

                  • Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string then repeat from 1.








                share|improve this answer





























                  1














                  public boolean isPalindrome(String abc){
                  if(abc != null && abc.length() > 0){
                  char arr = abc.toCharArray();
                  for (int i = 0; i < arr.length/2; i++) {
                  if(arr[i] != arr[arr.length - 1 - i]){
                  return false;
                  }
                  }
                  return true;
                  }
                  return false;
                  }





                  share|improve this answer





























                    1














                    Another way is using char Array



                    public class Palindrome {

                    public static void main(String args) {
                    String str = "madam";
                    if(isPalindrome(str)) {
                    System.out.println("Palindrome");
                    } else {
                    System.out.println("Not a Palindrome");
                    }
                    }

                    private static boolean isPalindrome(String str) {
                    // Convert String to char array
                    char charArray = str.toCharArray();
                    for(int i=0; i < str.length(); i++) {
                    if(charArray[i] != charArray[(str.length()-1) - i]) {
                    return false;
                    }
                    }
                    return true;
                    }


                    }






                    share|improve this answer

















                    • 1




                      This approach is excellent. Time Complexity O(n), space Complexity O(1)
                      – kanaparthikiran
                      Jan 1 at 4:06



















                    1














                    Here my analysis of the @Greg answer: componentsprogramming.com/palindromes





                    Sidenote: But, for me it is important to do it in a Generic way.
                    The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality.
                    I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.



                    template <BidirectionalIterator I> 
                    requires( EqualityComparable< ValueType<I> > )
                    bool palindrome( I first, I last )
                    {
                    I m = middle(first, last);
                    auto rfirst = boost::make_reverse_iterator(last);
                    return std::equal(first, m, rfirst);
                    }


                    Complexity: linear-time,




                    • If I is RandomAccessIterator:
                      floor(n/2) comparissons and floor(n/2)*2 iterations


                    • If I is BidirectionalIterator:
                      floor(n/2) comparissons and floor(n/2)*2 iterations
                      plus (3/2)*n iterations to find the middle ( middle function )


                    • storage: O(1)


                    • No dymamic allocated memory









                    share|improve this answer





























                      1














                      Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.



                      public boolean isPalindrome(String value) {
                      boolean isPalindrome = true;
                      for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
                      if (value.charAt(i) != value.charAt(j)) {
                      isPalindrome = false;
                      }
                      }
                      return isPalindrome;
                      }





                      share|improve this answer





























                        1














                        Using stack, it can be done like this



                        import java.io.*;
                        import java.util.*;
                        import java.text.*;
                        import java.math.*;
                        import java.util.regex.*;
                        import java.util.*;

                        public class Solution {

                        public static void main(String args) {
                        Scanner in = new Scanner(System.in);
                        String str=in.nextLine();
                        str.replaceAll("\s+","");
                        //System.out.println(str);
                        Stack<String> stack=new Stack<String>();
                        stack.push(str);
                        String str_rev=stack.pop();
                        if(str.equals(str_rev)){
                        System.out.println("Palindrome");
                        }else{
                        System.out.println("Not Palindrome");
                        }
                        }
                        }





                        share|improve this answer





















                        • thanks! can you explain the code ??
                          – danny
                          Apr 28 '17 at 11:14










                        • as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                          – aayushi
                          May 16 '17 at 19:42





















                        1














                         public static boolean isPalindrome(String word) {
                        String str = "";
                        for (int i=word.length()-1; i>=0; i--){
                        str = str + word.charAt(i);
                        }
                        if(str.equalsIgnoreCase(word)){
                        return true;
                        }else{
                        return false;
                        }

                        }





                        share|improve this answer





























                          1
















                          • This implementation works for numbers and strings.

                          • Since we are not writing anything, so there is no need to convert the string into the character array.




                          public static boolean isPalindrome(Object obj)
                          {
                          String s = String.valueOf(obj);

                          for(int left=0, right=s.length()-1; left < right; left++,right--)
                          {
                          if(s.charAt(left++) != s.charAt(right--))
                          return false;
                          }
                          return true;
                          }





                          share|improve this answer































                            0














                            import java.util.Scanner;


                            public class Palindrom {

                            public static void main(String args)
                            {
                            Scanner in = new Scanner(System.in);
                            String str= in.nextLine();
                            int x= str.length();

                            if(x%2!=0)
                            {
                            for(int i=0;i<x/2;i++)
                            {

                            if(str.charAt(i)==str.charAt(x-1-i))
                            {
                            continue;
                            }
                            else
                            {
                            System.out.println("String is not a palindrom");
                            break;
                            }
                            }
                            }
                            else
                            {
                            for(int i=0;i<=x/2;i++)
                            {
                            if(str.charAt(i)==str.charAt(x-1-i))
                            {
                            continue;
                            }
                            else
                            {
                            System.out.println("String is not a palindrom");
                            break;
                            }

                            }
                            }
                            }

                            }





                            share|improve this answer































                              0














                              private static boolean isPalindrome(String word) {

                              int z = word.length();
                              boolean isPalindrome = false;

                              for (int i = 0; i <= word.length() / 2; i++) {
                              if (word.charAt(i) == word.charAt(--z)) {
                              isPalindrome = true;
                              }
                              }

                              return isPalindrome;
                              }





                              share|improve this answer





























                                0














                                I was looking for a solution that not only worked for palindromes like...




                                • "Kayak"

                                • "Madam"


                                ...but as well for...




                                • "A man, a plan, a canal, Panama!"

                                • "Was it a car or a cat I saw?"

                                • "No 'x' in Nixon"


                                Iterative: This has be proven as a good solution.



                                private boolean isPalindromeIterative(final String string)
                                {
                                final char characters =
                                string.replaceAll("[\W]", "").toLowerCase().toCharArray();

                                int iteratorLeft = 0;
                                int iteratorEnd = characters.length - 1;

                                while (iteratorEnd > iteratorLeft)
                                {
                                if (characters[iteratorLeft++] != characters[iteratorEnd--])
                                {
                                return false;
                                }
                                }

                                return true;
                                }


                                Recursive. I think this solution shouldn't be much worse than the iterative one. Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.



                                private boolean isPalindromeRecursive(final String string)
                                {
                                final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                return isPalindromeRecursiveRecursion(cleanString);
                                }

                                private boolean isPalindromeRecursiveRecursion(final String cleanString)
                                {
                                final int cleanStringLength = cleanString.length();

                                return cleanStringLength <= 1 || cleanString.charAt(0) ==
                                cleanString.charAt(cleanStringLength - 1) &&
                                isPalindromeRecursiveRecursion
                                (cleanString.substring(1, cleanStringLength - 1));
                                }


                                Reversing: This has been proved as a expensive solution.



                                private boolean isPalindromeReversing(final String string)
                                {
                                final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
                                }


                                All the credits to the guys answering in this post and bringing light to the topic.






                                share|improve this answer





























                                  0














                                  Considering not letters in the words



                                  public static boolean palindromeWords(String s ){

                                  int left=0;
                                  int right=s.length()-1;

                                  while(left<=right){

                                  while(left<right && !Character.isLetter(s.charAt(left))){
                                  left++;
                                  }
                                  while(right>0 && !Character.isLetter(s.charAt(right))){
                                  right--;
                                  }

                                  if((s.charAt(left++))!=(s.charAt(right--))){
                                  return false;
                                  }
                                  }
                                  return true;
                                  }


                                  ———



                                  @Test
                                  public void testPalindromeWords(){
                                  assertTrue(StringExercise.palindromeWords("ece"));
                                  assertTrue(StringExercise.palindromeWords("kavak"));
                                  assertFalse(StringExercise.palindromeWords("kavakdf"));
                                  assertTrue(StringExercise.palindromeWords("akka"));
                                  assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
                                  }





                                  share|improve this answer





























                                    0














                                    Here you can check palindrome a number of String dynamically



                                    import java.util.Scanner;

                                    public class Checkpalindrome {
                                    public static void main(String args) {
                                    String original, reverse = "";
                                    Scanner in = new Scanner(System.in);
                                    System.out.println("Enter How Many number of Input you want : ");
                                    int numOfInt = in.nextInt();
                                    original = in.nextLine();
                                    do {
                                    if (numOfInt == 0) {
                                    System.out.println("Your Input Conplete");
                                    }
                                    else {
                                    System.out.println("Enter a string to check palindrome");
                                    original = in.nextLine();

                                    StringBuffer buffer = new StringBuffer(original);
                                    reverse = buffer.reverse().toString();

                                    if (original.equalsIgnoreCase(reverse)) {
                                    System.out.println("The entered string is Palindrome:"+reverse);
                                    }
                                    else {
                                    System.out.println("The entered string is not Palindrome:"+reverse);
                                    }
                                    }
                                    numOfInt--;
                                    } while (numOfInt >= 0);
                                    }
                                    }





                                    share|improve this answer































                                      0














                                      IMO, the recursive way is the simplest and clearest.



                                      public static boolean isPal(String s)
                                      {
                                      if(s.length() == 0 || s.length() == 1)
                                      return true;
                                      if(s.charAt(0) == s.charAt(s.length()-1))
                                      return isPal(s.substring(1, s.length()-1));
                                      return false;
                                      }





                                      share|improve this answer

















                                      • 2




                                        This already has been used in an answer: Check string for palindrome (just a note)
                                        – Tom
                                        Jan 8 '17 at 22:00










                                      • sorry, I missed it.
                                        – john Smith
                                        Jan 9 '17 at 13:48



















                                      0














                                      here, checking for the largest palindrome in a string, always starting from 1st char.



                                      public static String largestPalindromeInString(String in) {
                                      int right = in.length() - 1;
                                      int left = 0;
                                      char word = in.toCharArray();
                                      while (right > left && word[right] != word[left]) {
                                      right--;
                                      }
                                      int lenght = right + 1;
                                      while (right > left && word[right] == word[left]) {

                                      left++;
                                      right--;

                                      }
                                      if (0 >= right - left) {
                                      return new String(Arrays.copyOf(word, lenght ));
                                      } else {
                                      return largestPalindromeInString(
                                      new String(Arrays.copyOf(word, in.length() - 1)));
                                      }
                                      }





                                      share|improve this answer





























                                        0














                                        Code Snippet:



                                        import java.util.Scanner;

                                        class main
                                        {
                                        public static void main(String args)
                                        {
                                        Scanner sc = new Scanner(System.in);
                                        String str = sc.next();
                                        String reverse = new StringBuffer(str).reverse().toString();

                                        if(str.equals(reverse))
                                        System.out.println("Pallindrome");
                                        else
                                        System.out.println("Not Pallindrome");
                                        }
                                        }





                                        share|improve this answer





























                                          0














                                          enter image description here



                                          import java.util.Collections;
                                          import java.util.HashSet;
                                          import java.util.Scanner;
                                          import java.util.Set;

                                          public class GetAllPalindromes
                                          {
                                          static Scanner in;

                                          public static void main(String args)
                                          {
                                          in = new Scanner(System.in);
                                          System.out.println("Enter a string n");
                                          String abc = in.nextLine();
                                          Set a = printAllPalindromes(abc);
                                          System.out.println("set is " + a);
                                          }

                                          public static Set<CharSequence> printAllPalindromes(String input)
                                          {
                                          if (input.length() <= 2) {
                                          return Collections.emptySet();
                                          }

                                          Set<CharSequence> out = new HashSet<CharSequence>();
                                          int length = input.length();

                                          for (int i = 1; i < length - 1; i++)
                                          {
                                          for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++)
                                          {
                                          if (input.charAt(j) == input.charAt(k)) {
                                          out.add(input.subSequence(j, k + 1));
                                          } else {
                                          break;
                                          }
                                          }
                                          }
                                          return out;
                                          }
                                          }

                                          **Get All Palindrome in s given string**


                                          Output
                                          D:Java>java GetAllPalindromes
                                          Enter a string



                                          Hello user nitin is my best friend wow !



                                          Answer is set is [nitin, nitin , wow , wow, iti]



                                          D:Java>






                                          share|improve this answer

























                                            1 2
                                            next


                                            protected by Community Apr 7 '13 at 17:01



                                            Thank you for your interest in this question.
                                            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                            Would you like to answer one of these unanswered questions instead?














                                            34 Answers
                                            34






                                            active

                                            oldest

                                            votes








                                            34 Answers
                                            34






                                            active

                                            oldest

                                            votes









                                            active

                                            oldest

                                            votes






                                            active

                                            oldest

                                            votes








                                            1 2
                                            next










                                            164














                                            Why not just:



                                            public static boolean istPalindrom(char word){
                                            int i1 = 0;
                                            int i2 = word.length - 1;
                                            while (i2 > i1) {
                                            if (word[i1] != word[i2]) {
                                            return false;
                                            }
                                            ++i1;
                                            --i2;
                                            }
                                            return true;
                                            }


                                            Example:



                                            Input is "andna".

                                            i1 will be 0 and i2 will be 4.



                                            First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).

                                            So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).

                                            Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.






                                            share|improve this answer



















                                            • 1




                                              instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                              – user0946076422
                                              Aug 15 '15 at 3:44










                                            • @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                              – Vijay Tholpadi
                                              Mar 10 '17 at 3:11






                                            • 2




                                              @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                              – dcp
                                              Mar 15 '17 at 12:44
















                                            164














                                            Why not just:



                                            public static boolean istPalindrom(char word){
                                            int i1 = 0;
                                            int i2 = word.length - 1;
                                            while (i2 > i1) {
                                            if (word[i1] != word[i2]) {
                                            return false;
                                            }
                                            ++i1;
                                            --i2;
                                            }
                                            return true;
                                            }


                                            Example:



                                            Input is "andna".

                                            i1 will be 0 and i2 will be 4.



                                            First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).

                                            So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).

                                            Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.






                                            share|improve this answer



















                                            • 1




                                              instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                              – user0946076422
                                              Aug 15 '15 at 3:44










                                            • @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                              – Vijay Tholpadi
                                              Mar 10 '17 at 3:11






                                            • 2




                                              @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                              – dcp
                                              Mar 15 '17 at 12:44














                                            164












                                            164








                                            164






                                            Why not just:



                                            public static boolean istPalindrom(char word){
                                            int i1 = 0;
                                            int i2 = word.length - 1;
                                            while (i2 > i1) {
                                            if (word[i1] != word[i2]) {
                                            return false;
                                            }
                                            ++i1;
                                            --i2;
                                            }
                                            return true;
                                            }


                                            Example:



                                            Input is "andna".

                                            i1 will be 0 and i2 will be 4.



                                            First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).

                                            So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).

                                            Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.






                                            share|improve this answer














                                            Why not just:



                                            public static boolean istPalindrom(char word){
                                            int i1 = 0;
                                            int i2 = word.length - 1;
                                            while (i2 > i1) {
                                            if (word[i1] != word[i2]) {
                                            return false;
                                            }
                                            ++i1;
                                            --i2;
                                            }
                                            return true;
                                            }


                                            Example:



                                            Input is "andna".

                                            i1 will be 0 and i2 will be 4.



                                            First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).

                                            So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).

                                            Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited May 18 '15 at 23:05









                                            Michael Myers

                                            154k37256280




                                            154k37256280










                                            answered Nov 9 '10 at 21:32









                                            dcp

                                            42.6k16119145




                                            42.6k16119145








                                            • 1




                                              instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                              – user0946076422
                                              Aug 15 '15 at 3:44










                                            • @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                              – Vijay Tholpadi
                                              Mar 10 '17 at 3:11






                                            • 2




                                              @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                              – dcp
                                              Mar 15 '17 at 12:44














                                            • 1




                                              instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                              – user0946076422
                                              Aug 15 '15 at 3:44










                                            • @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                              – Vijay Tholpadi
                                              Mar 10 '17 at 3:11






                                            • 2




                                              @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                              – dcp
                                              Mar 15 '17 at 12:44








                                            1




                                            1




                                            instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                            – user0946076422
                                            Aug 15 '15 at 3:44




                                            instead of pre increment (++i1 and --i2), we can also use post increment (i1++, i2--)result is same i think !
                                            – user0946076422
                                            Aug 15 '15 at 3:44












                                            @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                            – Vijay Tholpadi
                                            Mar 10 '17 at 3:11




                                            @user0946076422 Yes. I too felt that way. Would be great if OP has a different explanation.
                                            – Vijay Tholpadi
                                            Mar 10 '17 at 3:11




                                            2




                                            2




                                            @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                            – dcp
                                            Mar 15 '17 at 12:44




                                            @Vijay Tholpadi - It's just really a coding preference more than anything else. Post increment would accomplish same result in this particular example, but I always use pre increment unless there's a specific reason not to.
                                            – dcp
                                            Mar 15 '17 at 12:44













                                            111














                                            You can check if a string is a palindrome by comparing it to the reverse of itself:



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuilder(str).reverse().toString());
                                            }


                                            or for versions of Java earlier than 1.5,



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuffer().append(str).reverse().toString());
                                            }


                                            EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!






                                            share|improve this answer



















                                            • 10




                                              Compare the complexity of your algorithm with respect to others.
                                              – Fernando Pelliccioni
                                              Feb 3 '14 at 14:24






                                            • 2




                                              @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                              – aioobe
                                              Aug 23 '15 at 8:16






                                            • 1




                                              @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                              – aioobe
                                              Aug 26 '15 at 21:24






                                            • 15




                                              Here my analysis: componentsprogramming.com/palindromes
                                              – Fernando Pelliccioni
                                              Jun 6 '16 at 21:09






                                            • 1




                                              @FernandoPelliccioni nice analysis
                                              – Saravana
                                              Jul 12 '16 at 4:11
















                                            111














                                            You can check if a string is a palindrome by comparing it to the reverse of itself:



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuilder(str).reverse().toString());
                                            }


                                            or for versions of Java earlier than 1.5,



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuffer().append(str).reverse().toString());
                                            }


                                            EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!






                                            share|improve this answer



















                                            • 10




                                              Compare the complexity of your algorithm with respect to others.
                                              – Fernando Pelliccioni
                                              Feb 3 '14 at 14:24






                                            • 2




                                              @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                              – aioobe
                                              Aug 23 '15 at 8:16






                                            • 1




                                              @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                              – aioobe
                                              Aug 26 '15 at 21:24






                                            • 15




                                              Here my analysis: componentsprogramming.com/palindromes
                                              – Fernando Pelliccioni
                                              Jun 6 '16 at 21:09






                                            • 1




                                              @FernandoPelliccioni nice analysis
                                              – Saravana
                                              Jul 12 '16 at 4:11














                                            111












                                            111








                                            111






                                            You can check if a string is a palindrome by comparing it to the reverse of itself:



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuilder(str).reverse().toString());
                                            }


                                            or for versions of Java earlier than 1.5,



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuffer().append(str).reverse().toString());
                                            }


                                            EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!






                                            share|improve this answer














                                            You can check if a string is a palindrome by comparing it to the reverse of itself:



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuilder(str).reverse().toString());
                                            }


                                            or for versions of Java earlier than 1.5,



                                            public static boolean isPalindrome(String str) {
                                            return str.equals(new StringBuffer().append(str).reverse().toString());
                                            }


                                            EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jul 13 '16 at 20:10

























                                            answered Nov 9 '10 at 21:54









                                            Greg

                                            28.5k138297




                                            28.5k138297








                                            • 10




                                              Compare the complexity of your algorithm with respect to others.
                                              – Fernando Pelliccioni
                                              Feb 3 '14 at 14:24






                                            • 2




                                              @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                              – aioobe
                                              Aug 23 '15 at 8:16






                                            • 1




                                              @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                              – aioobe
                                              Aug 26 '15 at 21:24






                                            • 15




                                              Here my analysis: componentsprogramming.com/palindromes
                                              – Fernando Pelliccioni
                                              Jun 6 '16 at 21:09






                                            • 1




                                              @FernandoPelliccioni nice analysis
                                              – Saravana
                                              Jul 12 '16 at 4:11














                                            • 10




                                              Compare the complexity of your algorithm with respect to others.
                                              – Fernando Pelliccioni
                                              Feb 3 '14 at 14:24






                                            • 2




                                              @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                              – aioobe
                                              Aug 23 '15 at 8:16






                                            • 1




                                              @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                              – aioobe
                                              Aug 26 '15 at 21:24






                                            • 15




                                              Here my analysis: componentsprogramming.com/palindromes
                                              – Fernando Pelliccioni
                                              Jun 6 '16 at 21:09






                                            • 1




                                              @FernandoPelliccioni nice analysis
                                              – Saravana
                                              Jul 12 '16 at 4:11








                                            10




                                            10




                                            Compare the complexity of your algorithm with respect to others.
                                            – Fernando Pelliccioni
                                            Feb 3 '14 at 14:24




                                            Compare the complexity of your algorithm with respect to others.
                                            – Fernando Pelliccioni
                                            Feb 3 '14 at 14:24




                                            2




                                            2




                                            @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                            – aioobe
                                            Aug 23 '15 at 8:16




                                            @FernandoPelliccioni, I think it's the same complexity as the other solutions, no?
                                            – aioobe
                                            Aug 23 '15 at 8:16




                                            1




                                            1




                                            @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                            – aioobe
                                            Aug 26 '15 at 21:24




                                            @Fernando, as far as I can tell all answers have a linear complexity. Because of this there's no way to give a definitive answer to which solution is most efficient. You could run benchmarks but they would be specific for a particular JVM and JRE. Best of luck with your blog post. Looking forward to reading it.
                                            – aioobe
                                            Aug 26 '15 at 21:24




                                            15




                                            15




                                            Here my analysis: componentsprogramming.com/palindromes
                                            – Fernando Pelliccioni
                                            Jun 6 '16 at 21:09




                                            Here my analysis: componentsprogramming.com/palindromes
                                            – Fernando Pelliccioni
                                            Jun 6 '16 at 21:09




                                            1




                                            1




                                            @FernandoPelliccioni nice analysis
                                            – Saravana
                                            Jul 12 '16 at 4:11




                                            @FernandoPelliccioni nice analysis
                                            – Saravana
                                            Jul 12 '16 at 4:11











                                            54














                                            A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:



                                            boolean isPalindrome(String str) {    
                                            int n = str.length();
                                            for( int i = 0; i < n/2; i++ )
                                            if (str.charAt(i) != str.charAt(n-i-1)) return false;
                                            return true;
                                            }





                                            share|improve this answer




























                                              54














                                              A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:



                                              boolean isPalindrome(String str) {    
                                              int n = str.length();
                                              for( int i = 0; i < n/2; i++ )
                                              if (str.charAt(i) != str.charAt(n-i-1)) return false;
                                              return true;
                                              }





                                              share|improve this answer


























                                                54












                                                54








                                                54






                                                A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:



                                                boolean isPalindrome(String str) {    
                                                int n = str.length();
                                                for( int i = 0; i < n/2; i++ )
                                                if (str.charAt(i) != str.charAt(n-i-1)) return false;
                                                return true;
                                                }





                                                share|improve this answer














                                                A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:



                                                boolean isPalindrome(String str) {    
                                                int n = str.length();
                                                for( int i = 0; i < n/2; i++ )
                                                if (str.charAt(i) != str.charAt(n-i-1)) return false;
                                                return true;
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Aug 13 '13 at 6:01









                                                David Robles

                                                6,56453242




                                                6,56453242










                                                answered Feb 22 '13 at 6:42









                                                Andrew Mao

                                                22.3k1193182




                                                22.3k1193182























                                                    14














                                                    Alternatively, recursion.



                                                    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();

                                                    if (length < 2) // If the string only has 1 char or is empty
                                                    return true;
                                                    else {
                                                    // Check opposite ends of the string for equality
                                                    if (s.charAt(0) != s.charAt(length - 1))
                                                    return false;
                                                    // Function call for string with the two ends snipped off
                                                    else
                                                    return isPalindrome(s.substring(1, length - 1));
                                                    }
                                                    }




                                                    OR even shorter, if you'd like:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();
                                                    if (length < 2) return true;
                                                    else return s.charAt(0) != s.charAt(length - 1) ? false :
                                                    isPalindrome(s.substring(1, length - 1));
                                                    }





                                                    share|improve this answer



















                                                    • 2




                                                      Nice code, recursion makes it really easy and less lines on code.
                                                      – Akash5288
                                                      Jan 31 '17 at 6:35










                                                    • the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                      – vault
                                                      Apr 19 at 23:05
















                                                    14














                                                    Alternatively, recursion.



                                                    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();

                                                    if (length < 2) // If the string only has 1 char or is empty
                                                    return true;
                                                    else {
                                                    // Check opposite ends of the string for equality
                                                    if (s.charAt(0) != s.charAt(length - 1))
                                                    return false;
                                                    // Function call for string with the two ends snipped off
                                                    else
                                                    return isPalindrome(s.substring(1, length - 1));
                                                    }
                                                    }




                                                    OR even shorter, if you'd like:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();
                                                    if (length < 2) return true;
                                                    else return s.charAt(0) != s.charAt(length - 1) ? false :
                                                    isPalindrome(s.substring(1, length - 1));
                                                    }





                                                    share|improve this answer



















                                                    • 2




                                                      Nice code, recursion makes it really easy and less lines on code.
                                                      – Akash5288
                                                      Jan 31 '17 at 6:35










                                                    • the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                      – vault
                                                      Apr 19 at 23:05














                                                    14












                                                    14








                                                    14






                                                    Alternatively, recursion.



                                                    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();

                                                    if (length < 2) // If the string only has 1 char or is empty
                                                    return true;
                                                    else {
                                                    // Check opposite ends of the string for equality
                                                    if (s.charAt(0) != s.charAt(length - 1))
                                                    return false;
                                                    // Function call for string with the two ends snipped off
                                                    else
                                                    return isPalindrome(s.substring(1, length - 1));
                                                    }
                                                    }




                                                    OR even shorter, if you'd like:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();
                                                    if (length < 2) return true;
                                                    else return s.charAt(0) != s.charAt(length - 1) ? false :
                                                    isPalindrome(s.substring(1, length - 1));
                                                    }





                                                    share|improve this answer














                                                    Alternatively, recursion.



                                                    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();

                                                    if (length < 2) // If the string only has 1 char or is empty
                                                    return true;
                                                    else {
                                                    // Check opposite ends of the string for equality
                                                    if (s.charAt(0) != s.charAt(length - 1))
                                                    return false;
                                                    // Function call for string with the two ends snipped off
                                                    else
                                                    return isPalindrome(s.substring(1, length - 1));
                                                    }
                                                    }




                                                    OR even shorter, if you'd like:



                                                    private boolean isPalindrome(String s) {
                                                    int length = s.length();
                                                    if (length < 2) return true;
                                                    else return s.charAt(0) != s.charAt(length - 1) ? false :
                                                    isPalindrome(s.substring(1, length - 1));
                                                    }






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Oct 30 '16 at 13:46

























                                                    answered Oct 30 '16 at 13:39









                                                    Keith OYS

                                                    1,97152332




                                                    1,97152332








                                                    • 2




                                                      Nice code, recursion makes it really easy and less lines on code.
                                                      – Akash5288
                                                      Jan 31 '17 at 6:35










                                                    • the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                      – vault
                                                      Apr 19 at 23:05














                                                    • 2




                                                      Nice code, recursion makes it really easy and less lines on code.
                                                      – Akash5288
                                                      Jan 31 '17 at 6:35










                                                    • the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                      – vault
                                                      Apr 19 at 23:05








                                                    2




                                                    2




                                                    Nice code, recursion makes it really easy and less lines on code.
                                                    – Akash5288
                                                    Jan 31 '17 at 6:35




                                                    Nice code, recursion makes it really easy and less lines on code.
                                                    – Akash5288
                                                    Jan 31 '17 at 6:35












                                                    the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                    – vault
                                                    Apr 19 at 23:05




                                                    the shorter version can be simplified: return s.charAt(0) == s.charAt(l - 1) && isPalindrome(s.substring(1, l - 1));
                                                    – vault
                                                    Apr 19 at 23:05











                                                    8














                                                    Go, Java:



                                                    public boolean isPalindrome (String word) {
                                                    String myWord = word.replaceAll("\s+","");
                                                    String reverse = new StringBuffer(myWord).reverse().toString();
                                                    return reverse.equalsIgnoreCase(myWord);
                                                    }

                                                    isPalindrome("Never Odd or Even"); // True
                                                    isPalindrome("Never Odd or Even1"); // False





                                                    share|improve this answer


























                                                      8














                                                      Go, Java:



                                                      public boolean isPalindrome (String word) {
                                                      String myWord = word.replaceAll("\s+","");
                                                      String reverse = new StringBuffer(myWord).reverse().toString();
                                                      return reverse.equalsIgnoreCase(myWord);
                                                      }

                                                      isPalindrome("Never Odd or Even"); // True
                                                      isPalindrome("Never Odd or Even1"); // False





                                                      share|improve this answer
























                                                        8












                                                        8








                                                        8






                                                        Go, Java:



                                                        public boolean isPalindrome (String word) {
                                                        String myWord = word.replaceAll("\s+","");
                                                        String reverse = new StringBuffer(myWord).reverse().toString();
                                                        return reverse.equalsIgnoreCase(myWord);
                                                        }

                                                        isPalindrome("Never Odd or Even"); // True
                                                        isPalindrome("Never Odd or Even1"); // False





                                                        share|improve this answer












                                                        Go, Java:



                                                        public boolean isPalindrome (String word) {
                                                        String myWord = word.replaceAll("\s+","");
                                                        String reverse = new StringBuffer(myWord).reverse().toString();
                                                        return reverse.equalsIgnoreCase(myWord);
                                                        }

                                                        isPalindrome("Never Odd or Even"); // True
                                                        isPalindrome("Never Odd or Even1"); // False






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Mar 4 '14 at 8:44









                                                        Francisco Gutiérrez

                                                        9981022




                                                        9981022























                                                            4














                                                            public class Aufg1 {
                                                            public static void main(String args) {
                                                            String wort = "reliefpfpfeiller";
                                                            char warray = wort.toCharArray();
                                                            System.out.println(istPalindrom(warray));
                                                            }

                                                            public static boolean istPalindrom(char wort){
                                                            if(wort.length%2 == 0){
                                                            for(int i = 0; i < wort.length/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }else{
                                                            for(int i = 0; i < (wort.length-1)/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }
                                                            return true;
                                                            }
                                                            }





                                                            share|improve this answer



















                                                            • 2




                                                              simplified a bit. but I like dcp's answer!
                                                              – Casey
                                                              Nov 9 '10 at 21:36
















                                                            4














                                                            public class Aufg1 {
                                                            public static void main(String args) {
                                                            String wort = "reliefpfpfeiller";
                                                            char warray = wort.toCharArray();
                                                            System.out.println(istPalindrom(warray));
                                                            }

                                                            public static boolean istPalindrom(char wort){
                                                            if(wort.length%2 == 0){
                                                            for(int i = 0; i < wort.length/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }else{
                                                            for(int i = 0; i < (wort.length-1)/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }
                                                            return true;
                                                            }
                                                            }





                                                            share|improve this answer



















                                                            • 2




                                                              simplified a bit. but I like dcp's answer!
                                                              – Casey
                                                              Nov 9 '10 at 21:36














                                                            4












                                                            4








                                                            4






                                                            public class Aufg1 {
                                                            public static void main(String args) {
                                                            String wort = "reliefpfpfeiller";
                                                            char warray = wort.toCharArray();
                                                            System.out.println(istPalindrom(warray));
                                                            }

                                                            public static boolean istPalindrom(char wort){
                                                            if(wort.length%2 == 0){
                                                            for(int i = 0; i < wort.length/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }else{
                                                            for(int i = 0; i < (wort.length-1)/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }
                                                            return true;
                                                            }
                                                            }





                                                            share|improve this answer














                                                            public class Aufg1 {
                                                            public static void main(String args) {
                                                            String wort = "reliefpfpfeiller";
                                                            char warray = wort.toCharArray();
                                                            System.out.println(istPalindrom(warray));
                                                            }

                                                            public static boolean istPalindrom(char wort){
                                                            if(wort.length%2 == 0){
                                                            for(int i = 0; i < wort.length/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }else{
                                                            for(int i = 0; i < (wort.length-1)/2-1; i++){
                                                            if(wort[i] != wort[wort.length-i-1]){
                                                            return false;
                                                            }
                                                            }
                                                            }
                                                            return true;
                                                            }
                                                            }






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Nov 9 '10 at 21:41

























                                                            answered Nov 9 '10 at 21:35









                                                            Casey

                                                            6,8731559103




                                                            6,8731559103








                                                            • 2




                                                              simplified a bit. but I like dcp's answer!
                                                              – Casey
                                                              Nov 9 '10 at 21:36














                                                            • 2




                                                              simplified a bit. but I like dcp's answer!
                                                              – Casey
                                                              Nov 9 '10 at 21:36








                                                            2




                                                            2




                                                            simplified a bit. but I like dcp's answer!
                                                            – Casey
                                                            Nov 9 '10 at 21:36




                                                            simplified a bit. but I like dcp's answer!
                                                            – Casey
                                                            Nov 9 '10 at 21:36











                                                            4














                                                            also a different looking solution:



                                                            public static boolean isPalindrome(String s) {

                                                            for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {

                                                            if ( s.charAt(i) != s.charAt(j) ) {
                                                            return false;
                                                            }
                                                            }

                                                            return true;
                                                            }





                                                            share|improve this answer


























                                                              4














                                                              also a different looking solution:



                                                              public static boolean isPalindrome(String s) {

                                                              for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {

                                                              if ( s.charAt(i) != s.charAt(j) ) {
                                                              return false;
                                                              }
                                                              }

                                                              return true;
                                                              }





                                                              share|improve this answer
























                                                                4












                                                                4








                                                                4






                                                                also a different looking solution:



                                                                public static boolean isPalindrome(String s) {

                                                                for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {

                                                                if ( s.charAt(i) != s.charAt(j) ) {
                                                                return false;
                                                                }
                                                                }

                                                                return true;
                                                                }





                                                                share|improve this answer












                                                                also a different looking solution:



                                                                public static boolean isPalindrome(String s) {

                                                                for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {

                                                                if ( s.charAt(i) != s.charAt(j) ) {
                                                                return false;
                                                                }
                                                                }

                                                                return true;
                                                                }






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered May 26 '16 at 3:52









                                                                Mona Jalal

                                                                7,85926108208




                                                                7,85926108208























                                                                    4














                                                                    And here a complete Java 8 streaming solution. An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done.



                                                                    public static void main(String args) {
                                                                    for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
                                                                    System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
                                                                    }
                                                                    }

                                                                    public static boolean isPalindrome(String str) {
                                                                    return IntStream.range(0, str.length() / 2)
                                                                    .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
                                                                    }


                                                                    Output is:



                                                                    testing testset is palindrome=true
                                                                    testing none is palindrome=false
                                                                    testing andna is palindrome=true
                                                                    testing haah is palindrome=true
                                                                    testing habh is palindrome=false
                                                                    testing haaah is palindrome=true





                                                                    share|improve this answer





















                                                                    • Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                      – gil.fernandes
                                                                      Apr 5 at 9:47
















                                                                    4














                                                                    And here a complete Java 8 streaming solution. An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done.



                                                                    public static void main(String args) {
                                                                    for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
                                                                    System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
                                                                    }
                                                                    }

                                                                    public static boolean isPalindrome(String str) {
                                                                    return IntStream.range(0, str.length() / 2)
                                                                    .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
                                                                    }


                                                                    Output is:



                                                                    testing testset is palindrome=true
                                                                    testing none is palindrome=false
                                                                    testing andna is palindrome=true
                                                                    testing haah is palindrome=true
                                                                    testing habh is palindrome=false
                                                                    testing haaah is palindrome=true





                                                                    share|improve this answer





















                                                                    • Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                      – gil.fernandes
                                                                      Apr 5 at 9:47














                                                                    4












                                                                    4








                                                                    4






                                                                    And here a complete Java 8 streaming solution. An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done.



                                                                    public static void main(String args) {
                                                                    for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
                                                                    System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
                                                                    }
                                                                    }

                                                                    public static boolean isPalindrome(String str) {
                                                                    return IntStream.range(0, str.length() / 2)
                                                                    .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
                                                                    }


                                                                    Output is:



                                                                    testing testset is palindrome=true
                                                                    testing none is palindrome=false
                                                                    testing andna is palindrome=true
                                                                    testing haah is palindrome=true
                                                                    testing habh is palindrome=false
                                                                    testing haaah is palindrome=true





                                                                    share|improve this answer












                                                                    And here a complete Java 8 streaming solution. An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done.



                                                                    public static void main(String args) {
                                                                    for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
                                                                    System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
                                                                    }
                                                                    }

                                                                    public static boolean isPalindrome(String str) {
                                                                    return IntStream.range(0, str.length() / 2)
                                                                    .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
                                                                    }


                                                                    Output is:



                                                                    testing testset is palindrome=true
                                                                    testing none is palindrome=false
                                                                    testing andna is palindrome=true
                                                                    testing haah is palindrome=true
                                                                    testing habh is palindrome=false
                                                                    testing haaah is palindrome=true






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered May 2 '17 at 6:49









                                                                    wumpz

                                                                    4,60121522




                                                                    4,60121522












                                                                    • Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                      – gil.fernandes
                                                                      Apr 5 at 9:47


















                                                                    • Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                      – gil.fernandes
                                                                      Apr 5 at 9:47
















                                                                    Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                    – gil.fernandes
                                                                    Apr 5 at 9:47




                                                                    Why not allMatch with allMatch(i -> str.charAt(i) == str.charAt(str.length() - i - 1)) ?
                                                                    – gil.fernandes
                                                                    Apr 5 at 9:47











                                                                    3














                                                                    Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.



                                                                    public int isPalindrome(String a) {
                                                                    //Remove all spaces and non alpha characters
                                                                    String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
                                                                    //System.out.println(ab);

                                                                    for (int i=0; i<ab.length()/2; i++) {
                                                                    if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
                                                                    return 0;
                                                                    }
                                                                    }
                                                                    return 1;
                                                                    }





                                                                    share|improve this answer


























                                                                      3














                                                                      Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.



                                                                      public int isPalindrome(String a) {
                                                                      //Remove all spaces and non alpha characters
                                                                      String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
                                                                      //System.out.println(ab);

                                                                      for (int i=0; i<ab.length()/2; i++) {
                                                                      if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
                                                                      return 0;
                                                                      }
                                                                      }
                                                                      return 1;
                                                                      }





                                                                      share|improve this answer
























                                                                        3












                                                                        3








                                                                        3






                                                                        Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.



                                                                        public int isPalindrome(String a) {
                                                                        //Remove all spaces and non alpha characters
                                                                        String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
                                                                        //System.out.println(ab);

                                                                        for (int i=0; i<ab.length()/2; i++) {
                                                                        if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
                                                                        return 0;
                                                                        }
                                                                        }
                                                                        return 1;
                                                                        }





                                                                        share|improve this answer












                                                                        Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.



                                                                        public int isPalindrome(String a) {
                                                                        //Remove all spaces and non alpha characters
                                                                        String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
                                                                        //System.out.println(ab);

                                                                        for (int i=0; i<ab.length()/2; i++) {
                                                                        if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
                                                                        return 0;
                                                                        }
                                                                        }
                                                                        return 1;
                                                                        }






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Jun 2 '17 at 17:27









                                                                        Abhilash Muthuraj

                                                                        84472039




                                                                        84472039























                                                                            2














                                                                            public class palindrome {
                                                                            public static void main(String args) {
                                                                            StringBuffer strBuf1 = new StringBuffer("malayalam");
                                                                            StringBuffer strBuf2 = new StringBuffer("malayalam");
                                                                            strBuf2.reverse();


                                                                            System.out.println(strBuf2);
                                                                            System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
                                                                            if ((strBuf1.toString()).equals(strBuf2.toString()))
                                                                            System.out.println("palindrome");
                                                                            else
                                                                            System.out.println("not a palindrome");
                                                                            }


                                                                            }






                                                                            share|improve this answer


























                                                                              2














                                                                              public class palindrome {
                                                                              public static void main(String args) {
                                                                              StringBuffer strBuf1 = new StringBuffer("malayalam");
                                                                              StringBuffer strBuf2 = new StringBuffer("malayalam");
                                                                              strBuf2.reverse();


                                                                              System.out.println(strBuf2);
                                                                              System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
                                                                              if ((strBuf1.toString()).equals(strBuf2.toString()))
                                                                              System.out.println("palindrome");
                                                                              else
                                                                              System.out.println("not a palindrome");
                                                                              }


                                                                              }






                                                                              share|improve this answer
























                                                                                2












                                                                                2








                                                                                2






                                                                                public class palindrome {
                                                                                public static void main(String args) {
                                                                                StringBuffer strBuf1 = new StringBuffer("malayalam");
                                                                                StringBuffer strBuf2 = new StringBuffer("malayalam");
                                                                                strBuf2.reverse();


                                                                                System.out.println(strBuf2);
                                                                                System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
                                                                                if ((strBuf1.toString()).equals(strBuf2.toString()))
                                                                                System.out.println("palindrome");
                                                                                else
                                                                                System.out.println("not a palindrome");
                                                                                }


                                                                                }






                                                                                share|improve this answer












                                                                                public class palindrome {
                                                                                public static void main(String args) {
                                                                                StringBuffer strBuf1 = new StringBuffer("malayalam");
                                                                                StringBuffer strBuf2 = new StringBuffer("malayalam");
                                                                                strBuf2.reverse();


                                                                                System.out.println(strBuf2);
                                                                                System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
                                                                                if ((strBuf1.toString()).equals(strBuf2.toString()))
                                                                                System.out.println("palindrome");
                                                                                else
                                                                                System.out.println("not a palindrome");
                                                                                }


                                                                                }







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Feb 8 '13 at 12:50









                                                                                user2039532

                                                                                215




                                                                                215























                                                                                    2














                                                                                    I worked on a solution for a question that was marked as duplicate of this one.
                                                                                    Might as well throw it here...



                                                                                    The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.



                                                                                    Here's the ugly solution with a small test class:



                                                                                    public class Palindrome {
                                                                                    public static boolean isPalendrome(String arg) {
                                                                                    return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
                                                                                    }
                                                                                    public static void main(String args) {
                                                                                    System.out.println(isPalendrome("hiya"));
                                                                                    System.out.println(isPalendrome("star buttons not tub rats"));
                                                                                    System.out.println(isPalendrome("stab nail at ill Italian bats!"));
                                                                                    return;
                                                                                    }
                                                                                    }


                                                                                    Sorry that it is kind of nasty - but the other question specified a one-liner.






                                                                                    share|improve this answer



















                                                                                    • 1




                                                                                      Just curious, why the ternary operator at the end?
                                                                                      – typingduck
                                                                                      Apr 17 '17 at 16:24










                                                                                    • Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                      – Marc
                                                                                      May 24 '17 at 15:51
















                                                                                    2














                                                                                    I worked on a solution for a question that was marked as duplicate of this one.
                                                                                    Might as well throw it here...



                                                                                    The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.



                                                                                    Here's the ugly solution with a small test class:



                                                                                    public class Palindrome {
                                                                                    public static boolean isPalendrome(String arg) {
                                                                                    return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
                                                                                    }
                                                                                    public static void main(String args) {
                                                                                    System.out.println(isPalendrome("hiya"));
                                                                                    System.out.println(isPalendrome("star buttons not tub rats"));
                                                                                    System.out.println(isPalendrome("stab nail at ill Italian bats!"));
                                                                                    return;
                                                                                    }
                                                                                    }


                                                                                    Sorry that it is kind of nasty - but the other question specified a one-liner.






                                                                                    share|improve this answer



















                                                                                    • 1




                                                                                      Just curious, why the ternary operator at the end?
                                                                                      – typingduck
                                                                                      Apr 17 '17 at 16:24










                                                                                    • Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                      – Marc
                                                                                      May 24 '17 at 15:51














                                                                                    2












                                                                                    2








                                                                                    2






                                                                                    I worked on a solution for a question that was marked as duplicate of this one.
                                                                                    Might as well throw it here...



                                                                                    The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.



                                                                                    Here's the ugly solution with a small test class:



                                                                                    public class Palindrome {
                                                                                    public static boolean isPalendrome(String arg) {
                                                                                    return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
                                                                                    }
                                                                                    public static void main(String args) {
                                                                                    System.out.println(isPalendrome("hiya"));
                                                                                    System.out.println(isPalendrome("star buttons not tub rats"));
                                                                                    System.out.println(isPalendrome("stab nail at ill Italian bats!"));
                                                                                    return;
                                                                                    }
                                                                                    }


                                                                                    Sorry that it is kind of nasty - but the other question specified a one-liner.






                                                                                    share|improve this answer














                                                                                    I worked on a solution for a question that was marked as duplicate of this one.
                                                                                    Might as well throw it here...



                                                                                    The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.



                                                                                    Here's the ugly solution with a small test class:



                                                                                    public class Palindrome {
                                                                                    public static boolean isPalendrome(String arg) {
                                                                                    return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
                                                                                    }
                                                                                    public static void main(String args) {
                                                                                    System.out.println(isPalendrome("hiya"));
                                                                                    System.out.println(isPalendrome("star buttons not tub rats"));
                                                                                    System.out.println(isPalendrome("stab nail at ill Italian bats!"));
                                                                                    return;
                                                                                    }
                                                                                    }


                                                                                    Sorry that it is kind of nasty - but the other question specified a one-liner.







                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited May 24 '17 at 15:52

























                                                                                    answered Jun 3 '11 at 13:28









                                                                                    Marc

                                                                                    2,40021521




                                                                                    2,40021521








                                                                                    • 1




                                                                                      Just curious, why the ternary operator at the end?
                                                                                      – typingduck
                                                                                      Apr 17 '17 at 16:24










                                                                                    • Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                      – Marc
                                                                                      May 24 '17 at 15:51














                                                                                    • 1




                                                                                      Just curious, why the ternary operator at the end?
                                                                                      – typingduck
                                                                                      Apr 17 '17 at 16:24










                                                                                    • Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                      – Marc
                                                                                      May 24 '17 at 15:51








                                                                                    1




                                                                                    1




                                                                                    Just curious, why the ternary operator at the end?
                                                                                    – typingduck
                                                                                    Apr 17 '17 at 16:24




                                                                                    Just curious, why the ternary operator at the end?
                                                                                    – typingduck
                                                                                    Apr 17 '17 at 16:24












                                                                                    Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                    – Marc
                                                                                    May 24 '17 at 15:51




                                                                                    Absolutely nothing - I must not have had my coffee. Will fix my response - thanks!.
                                                                                    – Marc
                                                                                    May 24 '17 at 15:51











                                                                                    2














                                                                                    Amazing how many different solutions to such a simple problem exist! Here's another one.



                                                                                    private static boolean palindrome(String s){
                                                                                    String revS = "";
                                                                                    String checkS = s.toLowerCase();
                                                                                    String checkSArr = checkS.split("");

                                                                                    for(String e : checkSArr){
                                                                                    revS = e + revS;
                                                                                    }

                                                                                    return (checkS.equals(revS)) ? true : false;
                                                                                    }





                                                                                    share|improve this answer


























                                                                                      2














                                                                                      Amazing how many different solutions to such a simple problem exist! Here's another one.



                                                                                      private static boolean palindrome(String s){
                                                                                      String revS = "";
                                                                                      String checkS = s.toLowerCase();
                                                                                      String checkSArr = checkS.split("");

                                                                                      for(String e : checkSArr){
                                                                                      revS = e + revS;
                                                                                      }

                                                                                      return (checkS.equals(revS)) ? true : false;
                                                                                      }





                                                                                      share|improve this answer
























                                                                                        2












                                                                                        2








                                                                                        2






                                                                                        Amazing how many different solutions to such a simple problem exist! Here's another one.



                                                                                        private static boolean palindrome(String s){
                                                                                        String revS = "";
                                                                                        String checkS = s.toLowerCase();
                                                                                        String checkSArr = checkS.split("");

                                                                                        for(String e : checkSArr){
                                                                                        revS = e + revS;
                                                                                        }

                                                                                        return (checkS.equals(revS)) ? true : false;
                                                                                        }





                                                                                        share|improve this answer












                                                                                        Amazing how many different solutions to such a simple problem exist! Here's another one.



                                                                                        private static boolean palindrome(String s){
                                                                                        String revS = "";
                                                                                        String checkS = s.toLowerCase();
                                                                                        String checkSArr = checkS.split("");

                                                                                        for(String e : checkSArr){
                                                                                        revS = e + revS;
                                                                                        }

                                                                                        return (checkS.equals(revS)) ? true : false;
                                                                                        }






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Jan 18 at 16:33









                                                                                        Felix

                                                                                        442317




                                                                                        442317























                                                                                            1














                                                                                            Try this out :



                                                                                            import java.util.*;
                                                                                            public class str {

                                                                                            public static void main(String args)
                                                                                            {
                                                                                            Scanner in=new Scanner(System.in);
                                                                                            System.out.println("ENTER YOUR STRING: ");
                                                                                            String a=in.nextLine();
                                                                                            System.out.println("GIVEN STRING IS: "+a);
                                                                                            StringBuffer str=new StringBuffer(a);
                                                                                            StringBuffer str2=new StringBuffer(str.reverse());
                                                                                            String s2=new String(str2);
                                                                                            System.out.println("THE REVERSED STRING IS: "+str2);
                                                                                            if(a.equals(s2))
                                                                                            System.out.println("ITS A PALINDROME");
                                                                                            else
                                                                                            System.out.println("ITS NOT A PALINDROME");
                                                                                            }
                                                                                            }





                                                                                            share|improve this answer




























                                                                                              1














                                                                                              Try this out :



                                                                                              import java.util.*;
                                                                                              public class str {

                                                                                              public static void main(String args)
                                                                                              {
                                                                                              Scanner in=new Scanner(System.in);
                                                                                              System.out.println("ENTER YOUR STRING: ");
                                                                                              String a=in.nextLine();
                                                                                              System.out.println("GIVEN STRING IS: "+a);
                                                                                              StringBuffer str=new StringBuffer(a);
                                                                                              StringBuffer str2=new StringBuffer(str.reverse());
                                                                                              String s2=new String(str2);
                                                                                              System.out.println("THE REVERSED STRING IS: "+str2);
                                                                                              if(a.equals(s2))
                                                                                              System.out.println("ITS A PALINDROME");
                                                                                              else
                                                                                              System.out.println("ITS NOT A PALINDROME");
                                                                                              }
                                                                                              }





                                                                                              share|improve this answer


























                                                                                                1












                                                                                                1








                                                                                                1






                                                                                                Try this out :



                                                                                                import java.util.*;
                                                                                                public class str {

                                                                                                public static void main(String args)
                                                                                                {
                                                                                                Scanner in=new Scanner(System.in);
                                                                                                System.out.println("ENTER YOUR STRING: ");
                                                                                                String a=in.nextLine();
                                                                                                System.out.println("GIVEN STRING IS: "+a);
                                                                                                StringBuffer str=new StringBuffer(a);
                                                                                                StringBuffer str2=new StringBuffer(str.reverse());
                                                                                                String s2=new String(str2);
                                                                                                System.out.println("THE REVERSED STRING IS: "+str2);
                                                                                                if(a.equals(s2))
                                                                                                System.out.println("ITS A PALINDROME");
                                                                                                else
                                                                                                System.out.println("ITS NOT A PALINDROME");
                                                                                                }
                                                                                                }





                                                                                                share|improve this answer














                                                                                                Try this out :



                                                                                                import java.util.*;
                                                                                                public class str {

                                                                                                public static void main(String args)
                                                                                                {
                                                                                                Scanner in=new Scanner(System.in);
                                                                                                System.out.println("ENTER YOUR STRING: ");
                                                                                                String a=in.nextLine();
                                                                                                System.out.println("GIVEN STRING IS: "+a);
                                                                                                StringBuffer str=new StringBuffer(a);
                                                                                                StringBuffer str2=new StringBuffer(str.reverse());
                                                                                                String s2=new String(str2);
                                                                                                System.out.println("THE REVERSED STRING IS: "+str2);
                                                                                                if(a.equals(s2))
                                                                                                System.out.println("ITS A PALINDROME");
                                                                                                else
                                                                                                System.out.println("ITS NOT A PALINDROME");
                                                                                                }
                                                                                                }






                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited Mar 12 '13 at 7:29









                                                                                                wizzardz

                                                                                                3,39743157




                                                                                                3,39743157










                                                                                                answered Mar 12 '13 at 7:19









                                                                                                ARAVIN

                                                                                                112




                                                                                                112























                                                                                                    1














                                                                                                    I'm new to java and I'm taking up your question as a challenge to improve my knowledge.



                                                                                                    import java.util.ArrayList;
                                                                                                    import java.util.List;

                                                                                                    public class PalindromeRecursiveBoolean {

                                                                                                    public static boolean isPalindrome(String str) {

                                                                                                    str = str.toUpperCase();
                                                                                                    char strChars = str.toCharArray();

                                                                                                    List<Character> word = new ArrayList<>();
                                                                                                    for (char c : strChars) {
                                                                                                    word.add(c);
                                                                                                    }

                                                                                                    while (true) {
                                                                                                    if ((word.size() == 1) || (word.size() == 0)) {
                                                                                                    return true;
                                                                                                    }
                                                                                                    if (word.get(0) == word.get(word.size() - 1)) {
                                                                                                    word.remove(0);
                                                                                                    word.remove(word.size() - 1);
                                                                                                    } else {
                                                                                                    return false;

                                                                                                    }

                                                                                                    }
                                                                                                    }
                                                                                                    }



                                                                                                    1. If the string is made of no letters or just one letter, it is a
                                                                                                      palindrome.

                                                                                                    2. Otherwise, compare the first and last letters of the string.

                                                                                                      • If the first and last letters differ, then the string is not a palindrome

                                                                                                      • Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string then repeat from 1.








                                                                                                    share|improve this answer


























                                                                                                      1














                                                                                                      I'm new to java and I'm taking up your question as a challenge to improve my knowledge.



                                                                                                      import java.util.ArrayList;
                                                                                                      import java.util.List;

                                                                                                      public class PalindromeRecursiveBoolean {

                                                                                                      public static boolean isPalindrome(String str) {

                                                                                                      str = str.toUpperCase();
                                                                                                      char strChars = str.toCharArray();

                                                                                                      List<Character> word = new ArrayList<>();
                                                                                                      for (char c : strChars) {
                                                                                                      word.add(c);
                                                                                                      }

                                                                                                      while (true) {
                                                                                                      if ((word.size() == 1) || (word.size() == 0)) {
                                                                                                      return true;
                                                                                                      }
                                                                                                      if (word.get(0) == word.get(word.size() - 1)) {
                                                                                                      word.remove(0);
                                                                                                      word.remove(word.size() - 1);
                                                                                                      } else {
                                                                                                      return false;

                                                                                                      }

                                                                                                      }
                                                                                                      }
                                                                                                      }



                                                                                                      1. If the string is made of no letters or just one letter, it is a
                                                                                                        palindrome.

                                                                                                      2. Otherwise, compare the first and last letters of the string.

                                                                                                        • If the first and last letters differ, then the string is not a palindrome

                                                                                                        • Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string then repeat from 1.








                                                                                                      share|improve this answer
























                                                                                                        1












                                                                                                        1








                                                                                                        1






                                                                                                        I'm new to java and I'm taking up your question as a challenge to improve my knowledge.



                                                                                                        import java.util.ArrayList;
                                                                                                        import java.util.List;

                                                                                                        public class PalindromeRecursiveBoolean {

                                                                                                        public static boolean isPalindrome(String str) {

                                                                                                        str = str.toUpperCase();
                                                                                                        char strChars = str.toCharArray();

                                                                                                        List<Character> word = new ArrayList<>();
                                                                                                        for (char c : strChars) {
                                                                                                        word.add(c);
                                                                                                        }

                                                                                                        while (true) {
                                                                                                        if ((word.size() == 1) || (word.size() == 0)) {
                                                                                                        return true;
                                                                                                        }
                                                                                                        if (word.get(0) == word.get(word.size() - 1)) {
                                                                                                        word.remove(0);
                                                                                                        word.remove(word.size() - 1);
                                                                                                        } else {
                                                                                                        return false;

                                                                                                        }

                                                                                                        }
                                                                                                        }
                                                                                                        }



                                                                                                        1. If the string is made of no letters or just one letter, it is a
                                                                                                          palindrome.

                                                                                                        2. Otherwise, compare the first and last letters of the string.

                                                                                                          • If the first and last letters differ, then the string is not a palindrome

                                                                                                          • Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string then repeat from 1.








                                                                                                        share|improve this answer












                                                                                                        I'm new to java and I'm taking up your question as a challenge to improve my knowledge.



                                                                                                        import java.util.ArrayList;
                                                                                                        import java.util.List;

                                                                                                        public class PalindromeRecursiveBoolean {

                                                                                                        public static boolean isPalindrome(String str) {

                                                                                                        str = str.toUpperCase();
                                                                                                        char strChars = str.toCharArray();

                                                                                                        List<Character> word = new ArrayList<>();
                                                                                                        for (char c : strChars) {
                                                                                                        word.add(c);
                                                                                                        }

                                                                                                        while (true) {
                                                                                                        if ((word.size() == 1) || (word.size() == 0)) {
                                                                                                        return true;
                                                                                                        }
                                                                                                        if (word.get(0) == word.get(word.size() - 1)) {
                                                                                                        word.remove(0);
                                                                                                        word.remove(word.size() - 1);
                                                                                                        } else {
                                                                                                        return false;

                                                                                                        }

                                                                                                        }
                                                                                                        }
                                                                                                        }



                                                                                                        1. If the string is made of no letters or just one letter, it is a
                                                                                                          palindrome.

                                                                                                        2. Otherwise, compare the first and last letters of the string.

                                                                                                          • If the first and last letters differ, then the string is not a palindrome

                                                                                                          • Otherwise, the first and last letters are the same. Strip them from the string, and determine whether the string that remains is a palindrome. Take the answer for this smaller string and use it as the answer for the original string then repeat from 1.









                                                                                                        share|improve this answer












                                                                                                        share|improve this answer



                                                                                                        share|improve this answer










                                                                                                        answered Dec 16 '14 at 2:08









                                                                                                        gogobebe2

                                                                                                        82214




                                                                                                        82214























                                                                                                            1














                                                                                                            public boolean isPalindrome(String abc){
                                                                                                            if(abc != null && abc.length() > 0){
                                                                                                            char arr = abc.toCharArray();
                                                                                                            for (int i = 0; i < arr.length/2; i++) {
                                                                                                            if(arr[i] != arr[arr.length - 1 - i]){
                                                                                                            return false;
                                                                                                            }
                                                                                                            }
                                                                                                            return true;
                                                                                                            }
                                                                                                            return false;
                                                                                                            }





                                                                                                            share|improve this answer


























                                                                                                              1














                                                                                                              public boolean isPalindrome(String abc){
                                                                                                              if(abc != null && abc.length() > 0){
                                                                                                              char arr = abc.toCharArray();
                                                                                                              for (int i = 0; i < arr.length/2; i++) {
                                                                                                              if(arr[i] != arr[arr.length - 1 - i]){
                                                                                                              return false;
                                                                                                              }
                                                                                                              }
                                                                                                              return true;
                                                                                                              }
                                                                                                              return false;
                                                                                                              }





                                                                                                              share|improve this answer
























                                                                                                                1












                                                                                                                1








                                                                                                                1






                                                                                                                public boolean isPalindrome(String abc){
                                                                                                                if(abc != null && abc.length() > 0){
                                                                                                                char arr = abc.toCharArray();
                                                                                                                for (int i = 0; i < arr.length/2; i++) {
                                                                                                                if(arr[i] != arr[arr.length - 1 - i]){
                                                                                                                return false;
                                                                                                                }
                                                                                                                }
                                                                                                                return true;
                                                                                                                }
                                                                                                                return false;
                                                                                                                }





                                                                                                                share|improve this answer












                                                                                                                public boolean isPalindrome(String abc){
                                                                                                                if(abc != null && abc.length() > 0){
                                                                                                                char arr = abc.toCharArray();
                                                                                                                for (int i = 0; i < arr.length/2; i++) {
                                                                                                                if(arr[i] != arr[arr.length - 1 - i]){
                                                                                                                return false;
                                                                                                                }
                                                                                                                }
                                                                                                                return true;
                                                                                                                }
                                                                                                                return false;
                                                                                                                }






                                                                                                                share|improve this answer












                                                                                                                share|improve this answer



                                                                                                                share|improve this answer










                                                                                                                answered May 28 '15 at 23:58









                                                                                                                Amandeep Dhanjal

                                                                                                                435




                                                                                                                435























                                                                                                                    1














                                                                                                                    Another way is using char Array



                                                                                                                    public class Palindrome {

                                                                                                                    public static void main(String args) {
                                                                                                                    String str = "madam";
                                                                                                                    if(isPalindrome(str)) {
                                                                                                                    System.out.println("Palindrome");
                                                                                                                    } else {
                                                                                                                    System.out.println("Not a Palindrome");
                                                                                                                    }
                                                                                                                    }

                                                                                                                    private static boolean isPalindrome(String str) {
                                                                                                                    // Convert String to char array
                                                                                                                    char charArray = str.toCharArray();
                                                                                                                    for(int i=0; i < str.length(); i++) {
                                                                                                                    if(charArray[i] != charArray[(str.length()-1) - i]) {
                                                                                                                    return false;
                                                                                                                    }
                                                                                                                    }
                                                                                                                    return true;
                                                                                                                    }


                                                                                                                    }






                                                                                                                    share|improve this answer

















                                                                                                                    • 1




                                                                                                                      This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                      – kanaparthikiran
                                                                                                                      Jan 1 at 4:06
















                                                                                                                    1














                                                                                                                    Another way is using char Array



                                                                                                                    public class Palindrome {

                                                                                                                    public static void main(String args) {
                                                                                                                    String str = "madam";
                                                                                                                    if(isPalindrome(str)) {
                                                                                                                    System.out.println("Palindrome");
                                                                                                                    } else {
                                                                                                                    System.out.println("Not a Palindrome");
                                                                                                                    }
                                                                                                                    }

                                                                                                                    private static boolean isPalindrome(String str) {
                                                                                                                    // Convert String to char array
                                                                                                                    char charArray = str.toCharArray();
                                                                                                                    for(int i=0; i < str.length(); i++) {
                                                                                                                    if(charArray[i] != charArray[(str.length()-1) - i]) {
                                                                                                                    return false;
                                                                                                                    }
                                                                                                                    }
                                                                                                                    return true;
                                                                                                                    }


                                                                                                                    }






                                                                                                                    share|improve this answer

















                                                                                                                    • 1




                                                                                                                      This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                      – kanaparthikiran
                                                                                                                      Jan 1 at 4:06














                                                                                                                    1












                                                                                                                    1








                                                                                                                    1






                                                                                                                    Another way is using char Array



                                                                                                                    public class Palindrome {

                                                                                                                    public static void main(String args) {
                                                                                                                    String str = "madam";
                                                                                                                    if(isPalindrome(str)) {
                                                                                                                    System.out.println("Palindrome");
                                                                                                                    } else {
                                                                                                                    System.out.println("Not a Palindrome");
                                                                                                                    }
                                                                                                                    }

                                                                                                                    private static boolean isPalindrome(String str) {
                                                                                                                    // Convert String to char array
                                                                                                                    char charArray = str.toCharArray();
                                                                                                                    for(int i=0; i < str.length(); i++) {
                                                                                                                    if(charArray[i] != charArray[(str.length()-1) - i]) {
                                                                                                                    return false;
                                                                                                                    }
                                                                                                                    }
                                                                                                                    return true;
                                                                                                                    }


                                                                                                                    }






                                                                                                                    share|improve this answer












                                                                                                                    Another way is using char Array



                                                                                                                    public class Palindrome {

                                                                                                                    public static void main(String args) {
                                                                                                                    String str = "madam";
                                                                                                                    if(isPalindrome(str)) {
                                                                                                                    System.out.println("Palindrome");
                                                                                                                    } else {
                                                                                                                    System.out.println("Not a Palindrome");
                                                                                                                    }
                                                                                                                    }

                                                                                                                    private static boolean isPalindrome(String str) {
                                                                                                                    // Convert String to char array
                                                                                                                    char charArray = str.toCharArray();
                                                                                                                    for(int i=0; i < str.length(); i++) {
                                                                                                                    if(charArray[i] != charArray[(str.length()-1) - i]) {
                                                                                                                    return false;
                                                                                                                    }
                                                                                                                    }
                                                                                                                    return true;
                                                                                                                    }


                                                                                                                    }







                                                                                                                    share|improve this answer












                                                                                                                    share|improve this answer



                                                                                                                    share|improve this answer










                                                                                                                    answered Jun 24 '15 at 11:18









                                                                                                                    Madura Harshana

                                                                                                                    80662036




                                                                                                                    80662036








                                                                                                                    • 1




                                                                                                                      This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                      – kanaparthikiran
                                                                                                                      Jan 1 at 4:06














                                                                                                                    • 1




                                                                                                                      This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                      – kanaparthikiran
                                                                                                                      Jan 1 at 4:06








                                                                                                                    1




                                                                                                                    1




                                                                                                                    This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                    – kanaparthikiran
                                                                                                                    Jan 1 at 4:06




                                                                                                                    This approach is excellent. Time Complexity O(n), space Complexity O(1)
                                                                                                                    – kanaparthikiran
                                                                                                                    Jan 1 at 4:06











                                                                                                                    1














                                                                                                                    Here my analysis of the @Greg answer: componentsprogramming.com/palindromes





                                                                                                                    Sidenote: But, for me it is important to do it in a Generic way.
                                                                                                                    The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality.
                                                                                                                    I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.



                                                                                                                    template <BidirectionalIterator I> 
                                                                                                                    requires( EqualityComparable< ValueType<I> > )
                                                                                                                    bool palindrome( I first, I last )
                                                                                                                    {
                                                                                                                    I m = middle(first, last);
                                                                                                                    auto rfirst = boost::make_reverse_iterator(last);
                                                                                                                    return std::equal(first, m, rfirst);
                                                                                                                    }


                                                                                                                    Complexity: linear-time,




                                                                                                                    • If I is RandomAccessIterator:
                                                                                                                      floor(n/2) comparissons and floor(n/2)*2 iterations


                                                                                                                    • If I is BidirectionalIterator:
                                                                                                                      floor(n/2) comparissons and floor(n/2)*2 iterations
                                                                                                                      plus (3/2)*n iterations to find the middle ( middle function )


                                                                                                                    • storage: O(1)


                                                                                                                    • No dymamic allocated memory









                                                                                                                    share|improve this answer


























                                                                                                                      1














                                                                                                                      Here my analysis of the @Greg answer: componentsprogramming.com/palindromes





                                                                                                                      Sidenote: But, for me it is important to do it in a Generic way.
                                                                                                                      The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality.
                                                                                                                      I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.



                                                                                                                      template <BidirectionalIterator I> 
                                                                                                                      requires( EqualityComparable< ValueType<I> > )
                                                                                                                      bool palindrome( I first, I last )
                                                                                                                      {
                                                                                                                      I m = middle(first, last);
                                                                                                                      auto rfirst = boost::make_reverse_iterator(last);
                                                                                                                      return std::equal(first, m, rfirst);
                                                                                                                      }


                                                                                                                      Complexity: linear-time,




                                                                                                                      • If I is RandomAccessIterator:
                                                                                                                        floor(n/2) comparissons and floor(n/2)*2 iterations


                                                                                                                      • If I is BidirectionalIterator:
                                                                                                                        floor(n/2) comparissons and floor(n/2)*2 iterations
                                                                                                                        plus (3/2)*n iterations to find the middle ( middle function )


                                                                                                                      • storage: O(1)


                                                                                                                      • No dymamic allocated memory









                                                                                                                      share|improve this answer
























                                                                                                                        1












                                                                                                                        1








                                                                                                                        1






                                                                                                                        Here my analysis of the @Greg answer: componentsprogramming.com/palindromes





                                                                                                                        Sidenote: But, for me it is important to do it in a Generic way.
                                                                                                                        The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality.
                                                                                                                        I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.



                                                                                                                        template <BidirectionalIterator I> 
                                                                                                                        requires( EqualityComparable< ValueType<I> > )
                                                                                                                        bool palindrome( I first, I last )
                                                                                                                        {
                                                                                                                        I m = middle(first, last);
                                                                                                                        auto rfirst = boost::make_reverse_iterator(last);
                                                                                                                        return std::equal(first, m, rfirst);
                                                                                                                        }


                                                                                                                        Complexity: linear-time,




                                                                                                                        • If I is RandomAccessIterator:
                                                                                                                          floor(n/2) comparissons and floor(n/2)*2 iterations


                                                                                                                        • If I is BidirectionalIterator:
                                                                                                                          floor(n/2) comparissons and floor(n/2)*2 iterations
                                                                                                                          plus (3/2)*n iterations to find the middle ( middle function )


                                                                                                                        • storage: O(1)


                                                                                                                        • No dymamic allocated memory









                                                                                                                        share|improve this answer












                                                                                                                        Here my analysis of the @Greg answer: componentsprogramming.com/palindromes





                                                                                                                        Sidenote: But, for me it is important to do it in a Generic way.
                                                                                                                        The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality.
                                                                                                                        I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.



                                                                                                                        template <BidirectionalIterator I> 
                                                                                                                        requires( EqualityComparable< ValueType<I> > )
                                                                                                                        bool palindrome( I first, I last )
                                                                                                                        {
                                                                                                                        I m = middle(first, last);
                                                                                                                        auto rfirst = boost::make_reverse_iterator(last);
                                                                                                                        return std::equal(first, m, rfirst);
                                                                                                                        }


                                                                                                                        Complexity: linear-time,




                                                                                                                        • If I is RandomAccessIterator:
                                                                                                                          floor(n/2) comparissons and floor(n/2)*2 iterations


                                                                                                                        • If I is BidirectionalIterator:
                                                                                                                          floor(n/2) comparissons and floor(n/2)*2 iterations
                                                                                                                          plus (3/2)*n iterations to find the middle ( middle function )


                                                                                                                        • storage: O(1)


                                                                                                                        • No dymamic allocated memory










                                                                                                                        share|improve this answer












                                                                                                                        share|improve this answer



                                                                                                                        share|improve this answer










                                                                                                                        answered Nov 14 '16 at 14:28









                                                                                                                        Fernando Pelliccioni

                                                                                                                        585614




                                                                                                                        585614























                                                                                                                            1














                                                                                                                            Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.



                                                                                                                            public boolean isPalindrome(String value) {
                                                                                                                            boolean isPalindrome = true;
                                                                                                                            for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
                                                                                                                            if (value.charAt(i) != value.charAt(j)) {
                                                                                                                            isPalindrome = false;
                                                                                                                            }
                                                                                                                            }
                                                                                                                            return isPalindrome;
                                                                                                                            }





                                                                                                                            share|improve this answer


























                                                                                                                              1














                                                                                                                              Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.



                                                                                                                              public boolean isPalindrome(String value) {
                                                                                                                              boolean isPalindrome = true;
                                                                                                                              for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
                                                                                                                              if (value.charAt(i) != value.charAt(j)) {
                                                                                                                              isPalindrome = false;
                                                                                                                              }
                                                                                                                              }
                                                                                                                              return isPalindrome;
                                                                                                                              }





                                                                                                                              share|improve this answer
























                                                                                                                                1












                                                                                                                                1








                                                                                                                                1






                                                                                                                                Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.



                                                                                                                                public boolean isPalindrome(String value) {
                                                                                                                                boolean isPalindrome = true;
                                                                                                                                for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
                                                                                                                                if (value.charAt(i) != value.charAt(j)) {
                                                                                                                                isPalindrome = false;
                                                                                                                                }
                                                                                                                                }
                                                                                                                                return isPalindrome;
                                                                                                                                }





                                                                                                                                share|improve this answer












                                                                                                                                Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.



                                                                                                                                public boolean isPalindrome(String value) {
                                                                                                                                boolean isPalindrome = true;
                                                                                                                                for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
                                                                                                                                if (value.charAt(i) != value.charAt(j)) {
                                                                                                                                isPalindrome = false;
                                                                                                                                }
                                                                                                                                }
                                                                                                                                return isPalindrome;
                                                                                                                                }






                                                                                                                                share|improve this answer












                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer










                                                                                                                                answered Apr 20 '17 at 6:56









                                                                                                                                capt.swag

                                                                                                                                5,95212425




                                                                                                                                5,95212425























                                                                                                                                    1














                                                                                                                                    Using stack, it can be done like this



                                                                                                                                    import java.io.*;
                                                                                                                                    import java.util.*;
                                                                                                                                    import java.text.*;
                                                                                                                                    import java.math.*;
                                                                                                                                    import java.util.regex.*;
                                                                                                                                    import java.util.*;

                                                                                                                                    public class Solution {

                                                                                                                                    public static void main(String args) {
                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                    String str=in.nextLine();
                                                                                                                                    str.replaceAll("\s+","");
                                                                                                                                    //System.out.println(str);
                                                                                                                                    Stack<String> stack=new Stack<String>();
                                                                                                                                    stack.push(str);
                                                                                                                                    String str_rev=stack.pop();
                                                                                                                                    if(str.equals(str_rev)){
                                                                                                                                    System.out.println("Palindrome");
                                                                                                                                    }else{
                                                                                                                                    System.out.println("Not Palindrome");
                                                                                                                                    }
                                                                                                                                    }
                                                                                                                                    }





                                                                                                                                    share|improve this answer





















                                                                                                                                    • thanks! can you explain the code ??
                                                                                                                                      – danny
                                                                                                                                      Apr 28 '17 at 11:14










                                                                                                                                    • as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                      – aayushi
                                                                                                                                      May 16 '17 at 19:42


















                                                                                                                                    1














                                                                                                                                    Using stack, it can be done like this



                                                                                                                                    import java.io.*;
                                                                                                                                    import java.util.*;
                                                                                                                                    import java.text.*;
                                                                                                                                    import java.math.*;
                                                                                                                                    import java.util.regex.*;
                                                                                                                                    import java.util.*;

                                                                                                                                    public class Solution {

                                                                                                                                    public static void main(String args) {
                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                    String str=in.nextLine();
                                                                                                                                    str.replaceAll("\s+","");
                                                                                                                                    //System.out.println(str);
                                                                                                                                    Stack<String> stack=new Stack<String>();
                                                                                                                                    stack.push(str);
                                                                                                                                    String str_rev=stack.pop();
                                                                                                                                    if(str.equals(str_rev)){
                                                                                                                                    System.out.println("Palindrome");
                                                                                                                                    }else{
                                                                                                                                    System.out.println("Not Palindrome");
                                                                                                                                    }
                                                                                                                                    }
                                                                                                                                    }





                                                                                                                                    share|improve this answer





















                                                                                                                                    • thanks! can you explain the code ??
                                                                                                                                      – danny
                                                                                                                                      Apr 28 '17 at 11:14










                                                                                                                                    • as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                      – aayushi
                                                                                                                                      May 16 '17 at 19:42
















                                                                                                                                    1












                                                                                                                                    1








                                                                                                                                    1






                                                                                                                                    Using stack, it can be done like this



                                                                                                                                    import java.io.*;
                                                                                                                                    import java.util.*;
                                                                                                                                    import java.text.*;
                                                                                                                                    import java.math.*;
                                                                                                                                    import java.util.regex.*;
                                                                                                                                    import java.util.*;

                                                                                                                                    public class Solution {

                                                                                                                                    public static void main(String args) {
                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                    String str=in.nextLine();
                                                                                                                                    str.replaceAll("\s+","");
                                                                                                                                    //System.out.println(str);
                                                                                                                                    Stack<String> stack=new Stack<String>();
                                                                                                                                    stack.push(str);
                                                                                                                                    String str_rev=stack.pop();
                                                                                                                                    if(str.equals(str_rev)){
                                                                                                                                    System.out.println("Palindrome");
                                                                                                                                    }else{
                                                                                                                                    System.out.println("Not Palindrome");
                                                                                                                                    }
                                                                                                                                    }
                                                                                                                                    }





                                                                                                                                    share|improve this answer












                                                                                                                                    Using stack, it can be done like this



                                                                                                                                    import java.io.*;
                                                                                                                                    import java.util.*;
                                                                                                                                    import java.text.*;
                                                                                                                                    import java.math.*;
                                                                                                                                    import java.util.regex.*;
                                                                                                                                    import java.util.*;

                                                                                                                                    public class Solution {

                                                                                                                                    public static void main(String args) {
                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                    String str=in.nextLine();
                                                                                                                                    str.replaceAll("\s+","");
                                                                                                                                    //System.out.println(str);
                                                                                                                                    Stack<String> stack=new Stack<String>();
                                                                                                                                    stack.push(str);
                                                                                                                                    String str_rev=stack.pop();
                                                                                                                                    if(str.equals(str_rev)){
                                                                                                                                    System.out.println("Palindrome");
                                                                                                                                    }else{
                                                                                                                                    System.out.println("Not Palindrome");
                                                                                                                                    }
                                                                                                                                    }
                                                                                                                                    }






                                                                                                                                    share|improve this answer












                                                                                                                                    share|improve this answer



                                                                                                                                    share|improve this answer










                                                                                                                                    answered Apr 20 '17 at 7:24









                                                                                                                                    aayushi

                                                                                                                                    15627




                                                                                                                                    15627












                                                                                                                                    • thanks! can you explain the code ??
                                                                                                                                      – danny
                                                                                                                                      Apr 28 '17 at 11:14










                                                                                                                                    • as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                      – aayushi
                                                                                                                                      May 16 '17 at 19:42




















                                                                                                                                    • thanks! can you explain the code ??
                                                                                                                                      – danny
                                                                                                                                      Apr 28 '17 at 11:14










                                                                                                                                    • as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                      – aayushi
                                                                                                                                      May 16 '17 at 19:42


















                                                                                                                                    thanks! can you explain the code ??
                                                                                                                                    – danny
                                                                                                                                    Apr 28 '17 at 11:14




                                                                                                                                    thanks! can you explain the code ??
                                                                                                                                    – danny
                                                                                                                                    Apr 28 '17 at 11:14












                                                                                                                                    as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                    – aayushi
                                                                                                                                    May 16 '17 at 19:42






                                                                                                                                    as you must be knowing that, stack is of LIFO type that means you are basically pushing data through the beginning of the stack and retrieving data from the end of stack using pop(). Hope this helps!
                                                                                                                                    – aayushi
                                                                                                                                    May 16 '17 at 19:42













                                                                                                                                    1














                                                                                                                                     public static boolean isPalindrome(String word) {
                                                                                                                                    String str = "";
                                                                                                                                    for (int i=word.length()-1; i>=0; i--){
                                                                                                                                    str = str + word.charAt(i);
                                                                                                                                    }
                                                                                                                                    if(str.equalsIgnoreCase(word)){
                                                                                                                                    return true;
                                                                                                                                    }else{
                                                                                                                                    return false;
                                                                                                                                    }

                                                                                                                                    }





                                                                                                                                    share|improve this answer


























                                                                                                                                      1














                                                                                                                                       public static boolean isPalindrome(String word) {
                                                                                                                                      String str = "";
                                                                                                                                      for (int i=word.length()-1; i>=0; i--){
                                                                                                                                      str = str + word.charAt(i);
                                                                                                                                      }
                                                                                                                                      if(str.equalsIgnoreCase(word)){
                                                                                                                                      return true;
                                                                                                                                      }else{
                                                                                                                                      return false;
                                                                                                                                      }

                                                                                                                                      }





                                                                                                                                      share|improve this answer
























                                                                                                                                        1












                                                                                                                                        1








                                                                                                                                        1






                                                                                                                                         public static boolean isPalindrome(String word) {
                                                                                                                                        String str = "";
                                                                                                                                        for (int i=word.length()-1; i>=0; i--){
                                                                                                                                        str = str + word.charAt(i);
                                                                                                                                        }
                                                                                                                                        if(str.equalsIgnoreCase(word)){
                                                                                                                                        return true;
                                                                                                                                        }else{
                                                                                                                                        return false;
                                                                                                                                        }

                                                                                                                                        }





                                                                                                                                        share|improve this answer












                                                                                                                                         public static boolean isPalindrome(String word) {
                                                                                                                                        String str = "";
                                                                                                                                        for (int i=word.length()-1; i>=0; i--){
                                                                                                                                        str = str + word.charAt(i);
                                                                                                                                        }
                                                                                                                                        if(str.equalsIgnoreCase(word)){
                                                                                                                                        return true;
                                                                                                                                        }else{
                                                                                                                                        return false;
                                                                                                                                        }

                                                                                                                                        }






                                                                                                                                        share|improve this answer












                                                                                                                                        share|improve this answer



                                                                                                                                        share|improve this answer










                                                                                                                                        answered May 26 '17 at 12:33









                                                                                                                                        Chandara Chea

                                                                                                                                        514




                                                                                                                                        514























                                                                                                                                            1
















                                                                                                                                            • This implementation works for numbers and strings.

                                                                                                                                            • Since we are not writing anything, so there is no need to convert the string into the character array.




                                                                                                                                            public static boolean isPalindrome(Object obj)
                                                                                                                                            {
                                                                                                                                            String s = String.valueOf(obj);

                                                                                                                                            for(int left=0, right=s.length()-1; left < right; left++,right--)
                                                                                                                                            {
                                                                                                                                            if(s.charAt(left++) != s.charAt(right--))
                                                                                                                                            return false;
                                                                                                                                            }
                                                                                                                                            return true;
                                                                                                                                            }





                                                                                                                                            share|improve this answer




























                                                                                                                                              1
















                                                                                                                                              • This implementation works for numbers and strings.

                                                                                                                                              • Since we are not writing anything, so there is no need to convert the string into the character array.




                                                                                                                                              public static boolean isPalindrome(Object obj)
                                                                                                                                              {
                                                                                                                                              String s = String.valueOf(obj);

                                                                                                                                              for(int left=0, right=s.length()-1; left < right; left++,right--)
                                                                                                                                              {
                                                                                                                                              if(s.charAt(left++) != s.charAt(right--))
                                                                                                                                              return false;
                                                                                                                                              }
                                                                                                                                              return true;
                                                                                                                                              }





                                                                                                                                              share|improve this answer


























                                                                                                                                                1












                                                                                                                                                1








                                                                                                                                                1








                                                                                                                                                • This implementation works for numbers and strings.

                                                                                                                                                • Since we are not writing anything, so there is no need to convert the string into the character array.




                                                                                                                                                public static boolean isPalindrome(Object obj)
                                                                                                                                                {
                                                                                                                                                String s = String.valueOf(obj);

                                                                                                                                                for(int left=0, right=s.length()-1; left < right; left++,right--)
                                                                                                                                                {
                                                                                                                                                if(s.charAt(left++) != s.charAt(right--))
                                                                                                                                                return false;
                                                                                                                                                }
                                                                                                                                                return true;
                                                                                                                                                }





                                                                                                                                                share|improve this answer
















                                                                                                                                                • This implementation works for numbers and strings.

                                                                                                                                                • Since we are not writing anything, so there is no need to convert the string into the character array.




                                                                                                                                                public static boolean isPalindrome(Object obj)
                                                                                                                                                {
                                                                                                                                                String s = String.valueOf(obj);

                                                                                                                                                for(int left=0, right=s.length()-1; left < right; left++,right--)
                                                                                                                                                {
                                                                                                                                                if(s.charAt(left++) != s.charAt(right--))
                                                                                                                                                return false;
                                                                                                                                                }
                                                                                                                                                return true;
                                                                                                                                                }






                                                                                                                                                share|improve this answer














                                                                                                                                                share|improve this answer



                                                                                                                                                share|improve this answer








                                                                                                                                                edited Dec 2 at 4:15

























                                                                                                                                                answered Dec 2 at 4:06









                                                                                                                                                Pratik Patil

                                                                                                                                                1,8551424




                                                                                                                                                1,8551424























                                                                                                                                                    0














                                                                                                                                                    import java.util.Scanner;


                                                                                                                                                    public class Palindrom {

                                                                                                                                                    public static void main(String args)
                                                                                                                                                    {
                                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                                    String str= in.nextLine();
                                                                                                                                                    int x= str.length();

                                                                                                                                                    if(x%2!=0)
                                                                                                                                                    {
                                                                                                                                                    for(int i=0;i<x/2;i++)
                                                                                                                                                    {

                                                                                                                                                    if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                    {
                                                                                                                                                    continue;
                                                                                                                                                    }
                                                                                                                                                    else
                                                                                                                                                    {
                                                                                                                                                    System.out.println("String is not a palindrom");
                                                                                                                                                    break;
                                                                                                                                                    }
                                                                                                                                                    }
                                                                                                                                                    }
                                                                                                                                                    else
                                                                                                                                                    {
                                                                                                                                                    for(int i=0;i<=x/2;i++)
                                                                                                                                                    {
                                                                                                                                                    if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                    {
                                                                                                                                                    continue;
                                                                                                                                                    }
                                                                                                                                                    else
                                                                                                                                                    {
                                                                                                                                                    System.out.println("String is not a palindrom");
                                                                                                                                                    break;
                                                                                                                                                    }

                                                                                                                                                    }
                                                                                                                                                    }
                                                                                                                                                    }

                                                                                                                                                    }





                                                                                                                                                    share|improve this answer




























                                                                                                                                                      0














                                                                                                                                                      import java.util.Scanner;


                                                                                                                                                      public class Palindrom {

                                                                                                                                                      public static void main(String args)
                                                                                                                                                      {
                                                                                                                                                      Scanner in = new Scanner(System.in);
                                                                                                                                                      String str= in.nextLine();
                                                                                                                                                      int x= str.length();

                                                                                                                                                      if(x%2!=0)
                                                                                                                                                      {
                                                                                                                                                      for(int i=0;i<x/2;i++)
                                                                                                                                                      {

                                                                                                                                                      if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                      {
                                                                                                                                                      continue;
                                                                                                                                                      }
                                                                                                                                                      else
                                                                                                                                                      {
                                                                                                                                                      System.out.println("String is not a palindrom");
                                                                                                                                                      break;
                                                                                                                                                      }
                                                                                                                                                      }
                                                                                                                                                      }
                                                                                                                                                      else
                                                                                                                                                      {
                                                                                                                                                      for(int i=0;i<=x/2;i++)
                                                                                                                                                      {
                                                                                                                                                      if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                      {
                                                                                                                                                      continue;
                                                                                                                                                      }
                                                                                                                                                      else
                                                                                                                                                      {
                                                                                                                                                      System.out.println("String is not a palindrom");
                                                                                                                                                      break;
                                                                                                                                                      }

                                                                                                                                                      }
                                                                                                                                                      }
                                                                                                                                                      }

                                                                                                                                                      }





                                                                                                                                                      share|improve this answer


























                                                                                                                                                        0












                                                                                                                                                        0








                                                                                                                                                        0






                                                                                                                                                        import java.util.Scanner;


                                                                                                                                                        public class Palindrom {

                                                                                                                                                        public static void main(String args)
                                                                                                                                                        {
                                                                                                                                                        Scanner in = new Scanner(System.in);
                                                                                                                                                        String str= in.nextLine();
                                                                                                                                                        int x= str.length();

                                                                                                                                                        if(x%2!=0)
                                                                                                                                                        {
                                                                                                                                                        for(int i=0;i<x/2;i++)
                                                                                                                                                        {

                                                                                                                                                        if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                        {
                                                                                                                                                        continue;
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        System.out.println("String is not a palindrom");
                                                                                                                                                        break;
                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        for(int i=0;i<=x/2;i++)
                                                                                                                                                        {
                                                                                                                                                        if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                        {
                                                                                                                                                        continue;
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        System.out.println("String is not a palindrom");
                                                                                                                                                        break;
                                                                                                                                                        }

                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        }

                                                                                                                                                        }





                                                                                                                                                        share|improve this answer














                                                                                                                                                        import java.util.Scanner;


                                                                                                                                                        public class Palindrom {

                                                                                                                                                        public static void main(String args)
                                                                                                                                                        {
                                                                                                                                                        Scanner in = new Scanner(System.in);
                                                                                                                                                        String str= in.nextLine();
                                                                                                                                                        int x= str.length();

                                                                                                                                                        if(x%2!=0)
                                                                                                                                                        {
                                                                                                                                                        for(int i=0;i<x/2;i++)
                                                                                                                                                        {

                                                                                                                                                        if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                        {
                                                                                                                                                        continue;
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        System.out.println("String is not a palindrom");
                                                                                                                                                        break;
                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        for(int i=0;i<=x/2;i++)
                                                                                                                                                        {
                                                                                                                                                        if(str.charAt(i)==str.charAt(x-1-i))
                                                                                                                                                        {
                                                                                                                                                        continue;
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                        System.out.println("String is not a palindrom");
                                                                                                                                                        break;
                                                                                                                                                        }

                                                                                                                                                        }
                                                                                                                                                        }
                                                                                                                                                        }

                                                                                                                                                        }






                                                                                                                                                        share|improve this answer














                                                                                                                                                        share|improve this answer



                                                                                                                                                        share|improve this answer








                                                                                                                                                        edited May 26 '16 at 3:53









                                                                                                                                                        Mona Jalal

                                                                                                                                                        7,85926108208




                                                                                                                                                        7,85926108208










                                                                                                                                                        answered Dec 4 '15 at 5:14









                                                                                                                                                        Nitesh

                                                                                                                                                        4629




                                                                                                                                                        4629























                                                                                                                                                            0














                                                                                                                                                            private static boolean isPalindrome(String word) {

                                                                                                                                                            int z = word.length();
                                                                                                                                                            boolean isPalindrome = false;

                                                                                                                                                            for (int i = 0; i <= word.length() / 2; i++) {
                                                                                                                                                            if (word.charAt(i) == word.charAt(--z)) {
                                                                                                                                                            isPalindrome = true;
                                                                                                                                                            }
                                                                                                                                                            }

                                                                                                                                                            return isPalindrome;
                                                                                                                                                            }





                                                                                                                                                            share|improve this answer


























                                                                                                                                                              0














                                                                                                                                                              private static boolean isPalindrome(String word) {

                                                                                                                                                              int z = word.length();
                                                                                                                                                              boolean isPalindrome = false;

                                                                                                                                                              for (int i = 0; i <= word.length() / 2; i++) {
                                                                                                                                                              if (word.charAt(i) == word.charAt(--z)) {
                                                                                                                                                              isPalindrome = true;
                                                                                                                                                              }
                                                                                                                                                              }

                                                                                                                                                              return isPalindrome;
                                                                                                                                                              }





                                                                                                                                                              share|improve this answer
























                                                                                                                                                                0












                                                                                                                                                                0








                                                                                                                                                                0






                                                                                                                                                                private static boolean isPalindrome(String word) {

                                                                                                                                                                int z = word.length();
                                                                                                                                                                boolean isPalindrome = false;

                                                                                                                                                                for (int i = 0; i <= word.length() / 2; i++) {
                                                                                                                                                                if (word.charAt(i) == word.charAt(--z)) {
                                                                                                                                                                isPalindrome = true;
                                                                                                                                                                }
                                                                                                                                                                }

                                                                                                                                                                return isPalindrome;
                                                                                                                                                                }





                                                                                                                                                                share|improve this answer












                                                                                                                                                                private static boolean isPalindrome(String word) {

                                                                                                                                                                int z = word.length();
                                                                                                                                                                boolean isPalindrome = false;

                                                                                                                                                                for (int i = 0; i <= word.length() / 2; i++) {
                                                                                                                                                                if (word.charAt(i) == word.charAt(--z)) {
                                                                                                                                                                isPalindrome = true;
                                                                                                                                                                }
                                                                                                                                                                }

                                                                                                                                                                return isPalindrome;
                                                                                                                                                                }






                                                                                                                                                                share|improve this answer












                                                                                                                                                                share|improve this answer



                                                                                                                                                                share|improve this answer










                                                                                                                                                                answered Oct 24 '16 at 7:43









                                                                                                                                                                Angela Sanchez

                                                                                                                                                                1351112




                                                                                                                                                                1351112























                                                                                                                                                                    0














                                                                                                                                                                    I was looking for a solution that not only worked for palindromes like...




                                                                                                                                                                    • "Kayak"

                                                                                                                                                                    • "Madam"


                                                                                                                                                                    ...but as well for...




                                                                                                                                                                    • "A man, a plan, a canal, Panama!"

                                                                                                                                                                    • "Was it a car or a cat I saw?"

                                                                                                                                                                    • "No 'x' in Nixon"


                                                                                                                                                                    Iterative: This has be proven as a good solution.



                                                                                                                                                                    private boolean isPalindromeIterative(final String string)
                                                                                                                                                                    {
                                                                                                                                                                    final char characters =
                                                                                                                                                                    string.replaceAll("[\W]", "").toLowerCase().toCharArray();

                                                                                                                                                                    int iteratorLeft = 0;
                                                                                                                                                                    int iteratorEnd = characters.length - 1;

                                                                                                                                                                    while (iteratorEnd > iteratorLeft)
                                                                                                                                                                    {
                                                                                                                                                                    if (characters[iteratorLeft++] != characters[iteratorEnd--])
                                                                                                                                                                    {
                                                                                                                                                                    return false;
                                                                                                                                                                    }
                                                                                                                                                                    }

                                                                                                                                                                    return true;
                                                                                                                                                                    }


                                                                                                                                                                    Recursive. I think this solution shouldn't be much worse than the iterative one. Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.



                                                                                                                                                                    private boolean isPalindromeRecursive(final String string)
                                                                                                                                                                    {
                                                                                                                                                                    final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                    return isPalindromeRecursiveRecursion(cleanString);
                                                                                                                                                                    }

                                                                                                                                                                    private boolean isPalindromeRecursiveRecursion(final String cleanString)
                                                                                                                                                                    {
                                                                                                                                                                    final int cleanStringLength = cleanString.length();

                                                                                                                                                                    return cleanStringLength <= 1 || cleanString.charAt(0) ==
                                                                                                                                                                    cleanString.charAt(cleanStringLength - 1) &&
                                                                                                                                                                    isPalindromeRecursiveRecursion
                                                                                                                                                                    (cleanString.substring(1, cleanStringLength - 1));
                                                                                                                                                                    }


                                                                                                                                                                    Reversing: This has been proved as a expensive solution.



                                                                                                                                                                    private boolean isPalindromeReversing(final String string)
                                                                                                                                                                    {
                                                                                                                                                                    final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                    return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
                                                                                                                                                                    }


                                                                                                                                                                    All the credits to the guys answering in this post and bringing light to the topic.






                                                                                                                                                                    share|improve this answer


























                                                                                                                                                                      0














                                                                                                                                                                      I was looking for a solution that not only worked for palindromes like...




                                                                                                                                                                      • "Kayak"

                                                                                                                                                                      • "Madam"


                                                                                                                                                                      ...but as well for...




                                                                                                                                                                      • "A man, a plan, a canal, Panama!"

                                                                                                                                                                      • "Was it a car or a cat I saw?"

                                                                                                                                                                      • "No 'x' in Nixon"


                                                                                                                                                                      Iterative: This has be proven as a good solution.



                                                                                                                                                                      private boolean isPalindromeIterative(final String string)
                                                                                                                                                                      {
                                                                                                                                                                      final char characters =
                                                                                                                                                                      string.replaceAll("[\W]", "").toLowerCase().toCharArray();

                                                                                                                                                                      int iteratorLeft = 0;
                                                                                                                                                                      int iteratorEnd = characters.length - 1;

                                                                                                                                                                      while (iteratorEnd > iteratorLeft)
                                                                                                                                                                      {
                                                                                                                                                                      if (characters[iteratorLeft++] != characters[iteratorEnd--])
                                                                                                                                                                      {
                                                                                                                                                                      return false;
                                                                                                                                                                      }
                                                                                                                                                                      }

                                                                                                                                                                      return true;
                                                                                                                                                                      }


                                                                                                                                                                      Recursive. I think this solution shouldn't be much worse than the iterative one. Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.



                                                                                                                                                                      private boolean isPalindromeRecursive(final String string)
                                                                                                                                                                      {
                                                                                                                                                                      final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                      return isPalindromeRecursiveRecursion(cleanString);
                                                                                                                                                                      }

                                                                                                                                                                      private boolean isPalindromeRecursiveRecursion(final String cleanString)
                                                                                                                                                                      {
                                                                                                                                                                      final int cleanStringLength = cleanString.length();

                                                                                                                                                                      return cleanStringLength <= 1 || cleanString.charAt(0) ==
                                                                                                                                                                      cleanString.charAt(cleanStringLength - 1) &&
                                                                                                                                                                      isPalindromeRecursiveRecursion
                                                                                                                                                                      (cleanString.substring(1, cleanStringLength - 1));
                                                                                                                                                                      }


                                                                                                                                                                      Reversing: This has been proved as a expensive solution.



                                                                                                                                                                      private boolean isPalindromeReversing(final String string)
                                                                                                                                                                      {
                                                                                                                                                                      final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                      return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
                                                                                                                                                                      }


                                                                                                                                                                      All the credits to the guys answering in this post and bringing light to the topic.






                                                                                                                                                                      share|improve this answer
























                                                                                                                                                                        0












                                                                                                                                                                        0








                                                                                                                                                                        0






                                                                                                                                                                        I was looking for a solution that not only worked for palindromes like...




                                                                                                                                                                        • "Kayak"

                                                                                                                                                                        • "Madam"


                                                                                                                                                                        ...but as well for...




                                                                                                                                                                        • "A man, a plan, a canal, Panama!"

                                                                                                                                                                        • "Was it a car or a cat I saw?"

                                                                                                                                                                        • "No 'x' in Nixon"


                                                                                                                                                                        Iterative: This has be proven as a good solution.



                                                                                                                                                                        private boolean isPalindromeIterative(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final char characters =
                                                                                                                                                                        string.replaceAll("[\W]", "").toLowerCase().toCharArray();

                                                                                                                                                                        int iteratorLeft = 0;
                                                                                                                                                                        int iteratorEnd = characters.length - 1;

                                                                                                                                                                        while (iteratorEnd > iteratorLeft)
                                                                                                                                                                        {
                                                                                                                                                                        if (characters[iteratorLeft++] != characters[iteratorEnd--])
                                                                                                                                                                        {
                                                                                                                                                                        return false;
                                                                                                                                                                        }
                                                                                                                                                                        }

                                                                                                                                                                        return true;
                                                                                                                                                                        }


                                                                                                                                                                        Recursive. I think this solution shouldn't be much worse than the iterative one. Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.



                                                                                                                                                                        private boolean isPalindromeRecursive(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                        return isPalindromeRecursiveRecursion(cleanString);
                                                                                                                                                                        }

                                                                                                                                                                        private boolean isPalindromeRecursiveRecursion(final String cleanString)
                                                                                                                                                                        {
                                                                                                                                                                        final int cleanStringLength = cleanString.length();

                                                                                                                                                                        return cleanStringLength <= 1 || cleanString.charAt(0) ==
                                                                                                                                                                        cleanString.charAt(cleanStringLength - 1) &&
                                                                                                                                                                        isPalindromeRecursiveRecursion
                                                                                                                                                                        (cleanString.substring(1, cleanStringLength - 1));
                                                                                                                                                                        }


                                                                                                                                                                        Reversing: This has been proved as a expensive solution.



                                                                                                                                                                        private boolean isPalindromeReversing(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                        return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
                                                                                                                                                                        }


                                                                                                                                                                        All the credits to the guys answering in this post and bringing light to the topic.






                                                                                                                                                                        share|improve this answer












                                                                                                                                                                        I was looking for a solution that not only worked for palindromes like...




                                                                                                                                                                        • "Kayak"

                                                                                                                                                                        • "Madam"


                                                                                                                                                                        ...but as well for...




                                                                                                                                                                        • "A man, a plan, a canal, Panama!"

                                                                                                                                                                        • "Was it a car or a cat I saw?"

                                                                                                                                                                        • "No 'x' in Nixon"


                                                                                                                                                                        Iterative: This has be proven as a good solution.



                                                                                                                                                                        private boolean isPalindromeIterative(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final char characters =
                                                                                                                                                                        string.replaceAll("[\W]", "").toLowerCase().toCharArray();

                                                                                                                                                                        int iteratorLeft = 0;
                                                                                                                                                                        int iteratorEnd = characters.length - 1;

                                                                                                                                                                        while (iteratorEnd > iteratorLeft)
                                                                                                                                                                        {
                                                                                                                                                                        if (characters[iteratorLeft++] != characters[iteratorEnd--])
                                                                                                                                                                        {
                                                                                                                                                                        return false;
                                                                                                                                                                        }
                                                                                                                                                                        }

                                                                                                                                                                        return true;
                                                                                                                                                                        }


                                                                                                                                                                        Recursive. I think this solution shouldn't be much worse than the iterative one. Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.



                                                                                                                                                                        private boolean isPalindromeRecursive(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                        return isPalindromeRecursiveRecursion(cleanString);
                                                                                                                                                                        }

                                                                                                                                                                        private boolean isPalindromeRecursiveRecursion(final String cleanString)
                                                                                                                                                                        {
                                                                                                                                                                        final int cleanStringLength = cleanString.length();

                                                                                                                                                                        return cleanStringLength <= 1 || cleanString.charAt(0) ==
                                                                                                                                                                        cleanString.charAt(cleanStringLength - 1) &&
                                                                                                                                                                        isPalindromeRecursiveRecursion
                                                                                                                                                                        (cleanString.substring(1, cleanStringLength - 1));
                                                                                                                                                                        }


                                                                                                                                                                        Reversing: This has been proved as a expensive solution.



                                                                                                                                                                        private boolean isPalindromeReversing(final String string)
                                                                                                                                                                        {
                                                                                                                                                                        final String cleanString = string.replaceAll("[\W]", "").toLowerCase();
                                                                                                                                                                        return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
                                                                                                                                                                        }


                                                                                                                                                                        All the credits to the guys answering in this post and bringing light to the topic.







                                                                                                                                                                        share|improve this answer












                                                                                                                                                                        share|improve this answer



                                                                                                                                                                        share|improve this answer










                                                                                                                                                                        answered Oct 24 '16 at 15:51









                                                                                                                                                                        Sotti

                                                                                                                                                                        10.6k23739




                                                                                                                                                                        10.6k23739























                                                                                                                                                                            0














                                                                                                                                                                            Considering not letters in the words



                                                                                                                                                                            public static boolean palindromeWords(String s ){

                                                                                                                                                                            int left=0;
                                                                                                                                                                            int right=s.length()-1;

                                                                                                                                                                            while(left<=right){

                                                                                                                                                                            while(left<right && !Character.isLetter(s.charAt(left))){
                                                                                                                                                                            left++;
                                                                                                                                                                            }
                                                                                                                                                                            while(right>0 && !Character.isLetter(s.charAt(right))){
                                                                                                                                                                            right--;
                                                                                                                                                                            }

                                                                                                                                                                            if((s.charAt(left++))!=(s.charAt(right--))){
                                                                                                                                                                            return false;
                                                                                                                                                                            }
                                                                                                                                                                            }
                                                                                                                                                                            return true;
                                                                                                                                                                            }


                                                                                                                                                                            ———



                                                                                                                                                                            @Test
                                                                                                                                                                            public void testPalindromeWords(){
                                                                                                                                                                            assertTrue(StringExercise.palindromeWords("ece"));
                                                                                                                                                                            assertTrue(StringExercise.palindromeWords("kavak"));
                                                                                                                                                                            assertFalse(StringExercise.palindromeWords("kavakdf"));
                                                                                                                                                                            assertTrue(StringExercise.palindromeWords("akka"));
                                                                                                                                                                            assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
                                                                                                                                                                            }





                                                                                                                                                                            share|improve this answer


























                                                                                                                                                                              0














                                                                                                                                                                              Considering not letters in the words



                                                                                                                                                                              public static boolean palindromeWords(String s ){

                                                                                                                                                                              int left=0;
                                                                                                                                                                              int right=s.length()-1;

                                                                                                                                                                              while(left<=right){

                                                                                                                                                                              while(left<right && !Character.isLetter(s.charAt(left))){
                                                                                                                                                                              left++;
                                                                                                                                                                              }
                                                                                                                                                                              while(right>0 && !Character.isLetter(s.charAt(right))){
                                                                                                                                                                              right--;
                                                                                                                                                                              }

                                                                                                                                                                              if((s.charAt(left++))!=(s.charAt(right--))){
                                                                                                                                                                              return false;
                                                                                                                                                                              }
                                                                                                                                                                              }
                                                                                                                                                                              return true;
                                                                                                                                                                              }


                                                                                                                                                                              ———



                                                                                                                                                                              @Test
                                                                                                                                                                              public void testPalindromeWords(){
                                                                                                                                                                              assertTrue(StringExercise.palindromeWords("ece"));
                                                                                                                                                                              assertTrue(StringExercise.palindromeWords("kavak"));
                                                                                                                                                                              assertFalse(StringExercise.palindromeWords("kavakdf"));
                                                                                                                                                                              assertTrue(StringExercise.palindromeWords("akka"));
                                                                                                                                                                              assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
                                                                                                                                                                              }





                                                                                                                                                                              share|improve this answer
























                                                                                                                                                                                0












                                                                                                                                                                                0








                                                                                                                                                                                0






                                                                                                                                                                                Considering not letters in the words



                                                                                                                                                                                public static boolean palindromeWords(String s ){

                                                                                                                                                                                int left=0;
                                                                                                                                                                                int right=s.length()-1;

                                                                                                                                                                                while(left<=right){

                                                                                                                                                                                while(left<right && !Character.isLetter(s.charAt(left))){
                                                                                                                                                                                left++;
                                                                                                                                                                                }
                                                                                                                                                                                while(right>0 && !Character.isLetter(s.charAt(right))){
                                                                                                                                                                                right--;
                                                                                                                                                                                }

                                                                                                                                                                                if((s.charAt(left++))!=(s.charAt(right--))){
                                                                                                                                                                                return false;
                                                                                                                                                                                }
                                                                                                                                                                                }
                                                                                                                                                                                return true;
                                                                                                                                                                                }


                                                                                                                                                                                ———



                                                                                                                                                                                @Test
                                                                                                                                                                                public void testPalindromeWords(){
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("ece"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("kavak"));
                                                                                                                                                                                assertFalse(StringExercise.palindromeWords("kavakdf"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("akka"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
                                                                                                                                                                                }





                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                Considering not letters in the words



                                                                                                                                                                                public static boolean palindromeWords(String s ){

                                                                                                                                                                                int left=0;
                                                                                                                                                                                int right=s.length()-1;

                                                                                                                                                                                while(left<=right){

                                                                                                                                                                                while(left<right && !Character.isLetter(s.charAt(left))){
                                                                                                                                                                                left++;
                                                                                                                                                                                }
                                                                                                                                                                                while(right>0 && !Character.isLetter(s.charAt(right))){
                                                                                                                                                                                right--;
                                                                                                                                                                                }

                                                                                                                                                                                if((s.charAt(left++))!=(s.charAt(right--))){
                                                                                                                                                                                return false;
                                                                                                                                                                                }
                                                                                                                                                                                }
                                                                                                                                                                                return true;
                                                                                                                                                                                }


                                                                                                                                                                                ———



                                                                                                                                                                                @Test
                                                                                                                                                                                public void testPalindromeWords(){
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("ece"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("kavak"));
                                                                                                                                                                                assertFalse(StringExercise.palindromeWords("kavakdf"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("akka"));
                                                                                                                                                                                assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
                                                                                                                                                                                }






                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                answered Dec 8 '16 at 7:44









                                                                                                                                                                                huseyin

                                                                                                                                                                                8601012




                                                                                                                                                                                8601012























                                                                                                                                                                                    0














                                                                                                                                                                                    Here you can check palindrome a number of String dynamically



                                                                                                                                                                                    import java.util.Scanner;

                                                                                                                                                                                    public class Checkpalindrome {
                                                                                                                                                                                    public static void main(String args) {
                                                                                                                                                                                    String original, reverse = "";
                                                                                                                                                                                    Scanner in = new Scanner(System.in);
                                                                                                                                                                                    System.out.println("Enter How Many number of Input you want : ");
                                                                                                                                                                                    int numOfInt = in.nextInt();
                                                                                                                                                                                    original = in.nextLine();
                                                                                                                                                                                    do {
                                                                                                                                                                                    if (numOfInt == 0) {
                                                                                                                                                                                    System.out.println("Your Input Conplete");
                                                                                                                                                                                    }
                                                                                                                                                                                    else {
                                                                                                                                                                                    System.out.println("Enter a string to check palindrome");
                                                                                                                                                                                    original = in.nextLine();

                                                                                                                                                                                    StringBuffer buffer = new StringBuffer(original);
                                                                                                                                                                                    reverse = buffer.reverse().toString();

                                                                                                                                                                                    if (original.equalsIgnoreCase(reverse)) {
                                                                                                                                                                                    System.out.println("The entered string is Palindrome:"+reverse);
                                                                                                                                                                                    }
                                                                                                                                                                                    else {
                                                                                                                                                                                    System.out.println("The entered string is not Palindrome:"+reverse);
                                                                                                                                                                                    }
                                                                                                                                                                                    }
                                                                                                                                                                                    numOfInt--;
                                                                                                                                                                                    } while (numOfInt >= 0);
                                                                                                                                                                                    }
                                                                                                                                                                                    }





                                                                                                                                                                                    share|improve this answer




























                                                                                                                                                                                      0














                                                                                                                                                                                      Here you can check palindrome a number of String dynamically



                                                                                                                                                                                      import java.util.Scanner;

                                                                                                                                                                                      public class Checkpalindrome {
                                                                                                                                                                                      public static void main(String args) {
                                                                                                                                                                                      String original, reverse = "";
                                                                                                                                                                                      Scanner in = new Scanner(System.in);
                                                                                                                                                                                      System.out.println("Enter How Many number of Input you want : ");
                                                                                                                                                                                      int numOfInt = in.nextInt();
                                                                                                                                                                                      original = in.nextLine();
                                                                                                                                                                                      do {
                                                                                                                                                                                      if (numOfInt == 0) {
                                                                                                                                                                                      System.out.println("Your Input Conplete");
                                                                                                                                                                                      }
                                                                                                                                                                                      else {
                                                                                                                                                                                      System.out.println("Enter a string to check palindrome");
                                                                                                                                                                                      original = in.nextLine();

                                                                                                                                                                                      StringBuffer buffer = new StringBuffer(original);
                                                                                                                                                                                      reverse = buffer.reverse().toString();

                                                                                                                                                                                      if (original.equalsIgnoreCase(reverse)) {
                                                                                                                                                                                      System.out.println("The entered string is Palindrome:"+reverse);
                                                                                                                                                                                      }
                                                                                                                                                                                      else {
                                                                                                                                                                                      System.out.println("The entered string is not Palindrome:"+reverse);
                                                                                                                                                                                      }
                                                                                                                                                                                      }
                                                                                                                                                                                      numOfInt--;
                                                                                                                                                                                      } while (numOfInt >= 0);
                                                                                                                                                                                      }
                                                                                                                                                                                      }





                                                                                                                                                                                      share|improve this answer


























                                                                                                                                                                                        0












                                                                                                                                                                                        0








                                                                                                                                                                                        0






                                                                                                                                                                                        Here you can check palindrome a number of String dynamically



                                                                                                                                                                                        import java.util.Scanner;

                                                                                                                                                                                        public class Checkpalindrome {
                                                                                                                                                                                        public static void main(String args) {
                                                                                                                                                                                        String original, reverse = "";
                                                                                                                                                                                        Scanner in = new Scanner(System.in);
                                                                                                                                                                                        System.out.println("Enter How Many number of Input you want : ");
                                                                                                                                                                                        int numOfInt = in.nextInt();
                                                                                                                                                                                        original = in.nextLine();
                                                                                                                                                                                        do {
                                                                                                                                                                                        if (numOfInt == 0) {
                                                                                                                                                                                        System.out.println("Your Input Conplete");
                                                                                                                                                                                        }
                                                                                                                                                                                        else {
                                                                                                                                                                                        System.out.println("Enter a string to check palindrome");
                                                                                                                                                                                        original = in.nextLine();

                                                                                                                                                                                        StringBuffer buffer = new StringBuffer(original);
                                                                                                                                                                                        reverse = buffer.reverse().toString();

                                                                                                                                                                                        if (original.equalsIgnoreCase(reverse)) {
                                                                                                                                                                                        System.out.println("The entered string is Palindrome:"+reverse);
                                                                                                                                                                                        }
                                                                                                                                                                                        else {
                                                                                                                                                                                        System.out.println("The entered string is not Palindrome:"+reverse);
                                                                                                                                                                                        }
                                                                                                                                                                                        }
                                                                                                                                                                                        numOfInt--;
                                                                                                                                                                                        } while (numOfInt >= 0);
                                                                                                                                                                                        }
                                                                                                                                                                                        }





                                                                                                                                                                                        share|improve this answer














                                                                                                                                                                                        Here you can check palindrome a number of String dynamically



                                                                                                                                                                                        import java.util.Scanner;

                                                                                                                                                                                        public class Checkpalindrome {
                                                                                                                                                                                        public static void main(String args) {
                                                                                                                                                                                        String original, reverse = "";
                                                                                                                                                                                        Scanner in = new Scanner(System.in);
                                                                                                                                                                                        System.out.println("Enter How Many number of Input you want : ");
                                                                                                                                                                                        int numOfInt = in.nextInt();
                                                                                                                                                                                        original = in.nextLine();
                                                                                                                                                                                        do {
                                                                                                                                                                                        if (numOfInt == 0) {
                                                                                                                                                                                        System.out.println("Your Input Conplete");
                                                                                                                                                                                        }
                                                                                                                                                                                        else {
                                                                                                                                                                                        System.out.println("Enter a string to check palindrome");
                                                                                                                                                                                        original = in.nextLine();

                                                                                                                                                                                        StringBuffer buffer = new StringBuffer(original);
                                                                                                                                                                                        reverse = buffer.reverse().toString();

                                                                                                                                                                                        if (original.equalsIgnoreCase(reverse)) {
                                                                                                                                                                                        System.out.println("The entered string is Palindrome:"+reverse);
                                                                                                                                                                                        }
                                                                                                                                                                                        else {
                                                                                                                                                                                        System.out.println("The entered string is not Palindrome:"+reverse);
                                                                                                                                                                                        }
                                                                                                                                                                                        }
                                                                                                                                                                                        numOfInt--;
                                                                                                                                                                                        } while (numOfInt >= 0);
                                                                                                                                                                                        }
                                                                                                                                                                                        }






                                                                                                                                                                                        share|improve this answer














                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                        share|improve this answer








                                                                                                                                                                                        edited Dec 23 '16 at 17:48

























                                                                                                                                                                                        answered Dec 23 '16 at 17:43









                                                                                                                                                                                        Md. Nasir Uddin

                                                                                                                                                                                        257




                                                                                                                                                                                        257























                                                                                                                                                                                            0














                                                                                                                                                                                            IMO, the recursive way is the simplest and clearest.



                                                                                                                                                                                            public static boolean isPal(String s)
                                                                                                                                                                                            {
                                                                                                                                                                                            if(s.length() == 0 || s.length() == 1)
                                                                                                                                                                                            return true;
                                                                                                                                                                                            if(s.charAt(0) == s.charAt(s.length()-1))
                                                                                                                                                                                            return isPal(s.substring(1, s.length()-1));
                                                                                                                                                                                            return false;
                                                                                                                                                                                            }





                                                                                                                                                                                            share|improve this answer

















                                                                                                                                                                                            • 2




                                                                                                                                                                                              This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                              – Tom
                                                                                                                                                                                              Jan 8 '17 at 22:00










                                                                                                                                                                                            • sorry, I missed it.
                                                                                                                                                                                              – john Smith
                                                                                                                                                                                              Jan 9 '17 at 13:48
















                                                                                                                                                                                            0














                                                                                                                                                                                            IMO, the recursive way is the simplest and clearest.



                                                                                                                                                                                            public static boolean isPal(String s)
                                                                                                                                                                                            {
                                                                                                                                                                                            if(s.length() == 0 || s.length() == 1)
                                                                                                                                                                                            return true;
                                                                                                                                                                                            if(s.charAt(0) == s.charAt(s.length()-1))
                                                                                                                                                                                            return isPal(s.substring(1, s.length()-1));
                                                                                                                                                                                            return false;
                                                                                                                                                                                            }





                                                                                                                                                                                            share|improve this answer

















                                                                                                                                                                                            • 2




                                                                                                                                                                                              This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                              – Tom
                                                                                                                                                                                              Jan 8 '17 at 22:00










                                                                                                                                                                                            • sorry, I missed it.
                                                                                                                                                                                              – john Smith
                                                                                                                                                                                              Jan 9 '17 at 13:48














                                                                                                                                                                                            0












                                                                                                                                                                                            0








                                                                                                                                                                                            0






                                                                                                                                                                                            IMO, the recursive way is the simplest and clearest.



                                                                                                                                                                                            public static boolean isPal(String s)
                                                                                                                                                                                            {
                                                                                                                                                                                            if(s.length() == 0 || s.length() == 1)
                                                                                                                                                                                            return true;
                                                                                                                                                                                            if(s.charAt(0) == s.charAt(s.length()-1))
                                                                                                                                                                                            return isPal(s.substring(1, s.length()-1));
                                                                                                                                                                                            return false;
                                                                                                                                                                                            }





                                                                                                                                                                                            share|improve this answer












                                                                                                                                                                                            IMO, the recursive way is the simplest and clearest.



                                                                                                                                                                                            public static boolean isPal(String s)
                                                                                                                                                                                            {
                                                                                                                                                                                            if(s.length() == 0 || s.length() == 1)
                                                                                                                                                                                            return true;
                                                                                                                                                                                            if(s.charAt(0) == s.charAt(s.length()-1))
                                                                                                                                                                                            return isPal(s.substring(1, s.length()-1));
                                                                                                                                                                                            return false;
                                                                                                                                                                                            }






                                                                                                                                                                                            share|improve this answer












                                                                                                                                                                                            share|improve this answer



                                                                                                                                                                                            share|improve this answer










                                                                                                                                                                                            answered Jan 8 '17 at 21:45









                                                                                                                                                                                            john Smith

                                                                                                                                                                                            70522243




                                                                                                                                                                                            70522243








                                                                                                                                                                                            • 2




                                                                                                                                                                                              This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                              – Tom
                                                                                                                                                                                              Jan 8 '17 at 22:00










                                                                                                                                                                                            • sorry, I missed it.
                                                                                                                                                                                              – john Smith
                                                                                                                                                                                              Jan 9 '17 at 13:48














                                                                                                                                                                                            • 2




                                                                                                                                                                                              This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                              – Tom
                                                                                                                                                                                              Jan 8 '17 at 22:00










                                                                                                                                                                                            • sorry, I missed it.
                                                                                                                                                                                              – john Smith
                                                                                                                                                                                              Jan 9 '17 at 13:48








                                                                                                                                                                                            2




                                                                                                                                                                                            2




                                                                                                                                                                                            This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                            – Tom
                                                                                                                                                                                            Jan 8 '17 at 22:00




                                                                                                                                                                                            This already has been used in an answer: Check string for palindrome (just a note)
                                                                                                                                                                                            – Tom
                                                                                                                                                                                            Jan 8 '17 at 22:00












                                                                                                                                                                                            sorry, I missed it.
                                                                                                                                                                                            – john Smith
                                                                                                                                                                                            Jan 9 '17 at 13:48




                                                                                                                                                                                            sorry, I missed it.
                                                                                                                                                                                            – john Smith
                                                                                                                                                                                            Jan 9 '17 at 13:48











                                                                                                                                                                                            0














                                                                                                                                                                                            here, checking for the largest palindrome in a string, always starting from 1st char.



                                                                                                                                                                                            public static String largestPalindromeInString(String in) {
                                                                                                                                                                                            int right = in.length() - 1;
                                                                                                                                                                                            int left = 0;
                                                                                                                                                                                            char word = in.toCharArray();
                                                                                                                                                                                            while (right > left && word[right] != word[left]) {
                                                                                                                                                                                            right--;
                                                                                                                                                                                            }
                                                                                                                                                                                            int lenght = right + 1;
                                                                                                                                                                                            while (right > left && word[right] == word[left]) {

                                                                                                                                                                                            left++;
                                                                                                                                                                                            right--;

                                                                                                                                                                                            }
                                                                                                                                                                                            if (0 >= right - left) {
                                                                                                                                                                                            return new String(Arrays.copyOf(word, lenght ));
                                                                                                                                                                                            } else {
                                                                                                                                                                                            return largestPalindromeInString(
                                                                                                                                                                                            new String(Arrays.copyOf(word, in.length() - 1)));
                                                                                                                                                                                            }
                                                                                                                                                                                            }





                                                                                                                                                                                            share|improve this answer


























                                                                                                                                                                                              0














                                                                                                                                                                                              here, checking for the largest palindrome in a string, always starting from 1st char.



                                                                                                                                                                                              public static String largestPalindromeInString(String in) {
                                                                                                                                                                                              int right = in.length() - 1;
                                                                                                                                                                                              int left = 0;
                                                                                                                                                                                              char word = in.toCharArray();
                                                                                                                                                                                              while (right > left && word[right] != word[left]) {
                                                                                                                                                                                              right--;
                                                                                                                                                                                              }
                                                                                                                                                                                              int lenght = right + 1;
                                                                                                                                                                                              while (right > left && word[right] == word[left]) {

                                                                                                                                                                                              left++;
                                                                                                                                                                                              right--;

                                                                                                                                                                                              }
                                                                                                                                                                                              if (0 >= right - left) {
                                                                                                                                                                                              return new String(Arrays.copyOf(word, lenght ));
                                                                                                                                                                                              } else {
                                                                                                                                                                                              return largestPalindromeInString(
                                                                                                                                                                                              new String(Arrays.copyOf(word, in.length() - 1)));
                                                                                                                                                                                              }
                                                                                                                                                                                              }





                                                                                                                                                                                              share|improve this answer
























                                                                                                                                                                                                0












                                                                                                                                                                                                0








                                                                                                                                                                                                0






                                                                                                                                                                                                here, checking for the largest palindrome in a string, always starting from 1st char.



                                                                                                                                                                                                public static String largestPalindromeInString(String in) {
                                                                                                                                                                                                int right = in.length() - 1;
                                                                                                                                                                                                int left = 0;
                                                                                                                                                                                                char word = in.toCharArray();
                                                                                                                                                                                                while (right > left && word[right] != word[left]) {
                                                                                                                                                                                                right--;
                                                                                                                                                                                                }
                                                                                                                                                                                                int lenght = right + 1;
                                                                                                                                                                                                while (right > left && word[right] == word[left]) {

                                                                                                                                                                                                left++;
                                                                                                                                                                                                right--;

                                                                                                                                                                                                }
                                                                                                                                                                                                if (0 >= right - left) {
                                                                                                                                                                                                return new String(Arrays.copyOf(word, lenght ));
                                                                                                                                                                                                } else {
                                                                                                                                                                                                return largestPalindromeInString(
                                                                                                                                                                                                new String(Arrays.copyOf(word, in.length() - 1)));
                                                                                                                                                                                                }
                                                                                                                                                                                                }





                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                here, checking for the largest palindrome in a string, always starting from 1st char.



                                                                                                                                                                                                public static String largestPalindromeInString(String in) {
                                                                                                                                                                                                int right = in.length() - 1;
                                                                                                                                                                                                int left = 0;
                                                                                                                                                                                                char word = in.toCharArray();
                                                                                                                                                                                                while (right > left && word[right] != word[left]) {
                                                                                                                                                                                                right--;
                                                                                                                                                                                                }
                                                                                                                                                                                                int lenght = right + 1;
                                                                                                                                                                                                while (right > left && word[right] == word[left]) {

                                                                                                                                                                                                left++;
                                                                                                                                                                                                right--;

                                                                                                                                                                                                }
                                                                                                                                                                                                if (0 >= right - left) {
                                                                                                                                                                                                return new String(Arrays.copyOf(word, lenght ));
                                                                                                                                                                                                } else {
                                                                                                                                                                                                return largestPalindromeInString(
                                                                                                                                                                                                new String(Arrays.copyOf(word, in.length() - 1)));
                                                                                                                                                                                                }
                                                                                                                                                                                                }






                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                                answered Feb 3 '17 at 16:21









                                                                                                                                                                                                juanmf

                                                                                                                                                                                                1,65411824




                                                                                                                                                                                                1,65411824























                                                                                                                                                                                                    0














                                                                                                                                                                                                    Code Snippet:



                                                                                                                                                                                                    import java.util.Scanner;

                                                                                                                                                                                                    class main
                                                                                                                                                                                                    {
                                                                                                                                                                                                    public static void main(String args)
                                                                                                                                                                                                    {
                                                                                                                                                                                                    Scanner sc = new Scanner(System.in);
                                                                                                                                                                                                    String str = sc.next();
                                                                                                                                                                                                    String reverse = new StringBuffer(str).reverse().toString();

                                                                                                                                                                                                    if(str.equals(reverse))
                                                                                                                                                                                                    System.out.println("Pallindrome");
                                                                                                                                                                                                    else
                                                                                                                                                                                                    System.out.println("Not Pallindrome");
                                                                                                                                                                                                    }
                                                                                                                                                                                                    }





                                                                                                                                                                                                    share|improve this answer


























                                                                                                                                                                                                      0














                                                                                                                                                                                                      Code Snippet:



                                                                                                                                                                                                      import java.util.Scanner;

                                                                                                                                                                                                      class main
                                                                                                                                                                                                      {
                                                                                                                                                                                                      public static void main(String args)
                                                                                                                                                                                                      {
                                                                                                                                                                                                      Scanner sc = new Scanner(System.in);
                                                                                                                                                                                                      String str = sc.next();
                                                                                                                                                                                                      String reverse = new StringBuffer(str).reverse().toString();

                                                                                                                                                                                                      if(str.equals(reverse))
                                                                                                                                                                                                      System.out.println("Pallindrome");
                                                                                                                                                                                                      else
                                                                                                                                                                                                      System.out.println("Not Pallindrome");
                                                                                                                                                                                                      }
                                                                                                                                                                                                      }





                                                                                                                                                                                                      share|improve this answer
























                                                                                                                                                                                                        0












                                                                                                                                                                                                        0








                                                                                                                                                                                                        0






                                                                                                                                                                                                        Code Snippet:



                                                                                                                                                                                                        import java.util.Scanner;

                                                                                                                                                                                                        class main
                                                                                                                                                                                                        {
                                                                                                                                                                                                        public static void main(String args)
                                                                                                                                                                                                        {
                                                                                                                                                                                                        Scanner sc = new Scanner(System.in);
                                                                                                                                                                                                        String str = sc.next();
                                                                                                                                                                                                        String reverse = new StringBuffer(str).reverse().toString();

                                                                                                                                                                                                        if(str.equals(reverse))
                                                                                                                                                                                                        System.out.println("Pallindrome");
                                                                                                                                                                                                        else
                                                                                                                                                                                                        System.out.println("Not Pallindrome");
                                                                                                                                                                                                        }
                                                                                                                                                                                                        }





                                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                                        Code Snippet:



                                                                                                                                                                                                        import java.util.Scanner;

                                                                                                                                                                                                        class main
                                                                                                                                                                                                        {
                                                                                                                                                                                                        public static void main(String args)
                                                                                                                                                                                                        {
                                                                                                                                                                                                        Scanner sc = new Scanner(System.in);
                                                                                                                                                                                                        String str = sc.next();
                                                                                                                                                                                                        String reverse = new StringBuffer(str).reverse().toString();

                                                                                                                                                                                                        if(str.equals(reverse))
                                                                                                                                                                                                        System.out.println("Pallindrome");
                                                                                                                                                                                                        else
                                                                                                                                                                                                        System.out.println("Not Pallindrome");
                                                                                                                                                                                                        }
                                                                                                                                                                                                        }






                                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                                        share|improve this answer










                                                                                                                                                                                                        answered Mar 28 '17 at 18:23









                                                                                                                                                                                                        rashedcs

                                                                                                                                                                                                        1,0641224




                                                                                                                                                                                                        1,0641224























                                                                                                                                                                                                            0














                                                                                                                                                                                                            enter image description here



                                                                                                                                                                                                            import java.util.Collections;
                                                                                                                                                                                                            import java.util.HashSet;
                                                                                                                                                                                                            import java.util.Scanner;
                                                                                                                                                                                                            import java.util.Set;

                                                                                                                                                                                                            public class GetAllPalindromes
                                                                                                                                                                                                            {
                                                                                                                                                                                                            static Scanner in;

                                                                                                                                                                                                            public static void main(String args)
                                                                                                                                                                                                            {
                                                                                                                                                                                                            in = new Scanner(System.in);
                                                                                                                                                                                                            System.out.println("Enter a string n");
                                                                                                                                                                                                            String abc = in.nextLine();
                                                                                                                                                                                                            Set a = printAllPalindromes(abc);
                                                                                                                                                                                                            System.out.println("set is " + a);
                                                                                                                                                                                                            }

                                                                                                                                                                                                            public static Set<CharSequence> printAllPalindromes(String input)
                                                                                                                                                                                                            {
                                                                                                                                                                                                            if (input.length() <= 2) {
                                                                                                                                                                                                            return Collections.emptySet();
                                                                                                                                                                                                            }

                                                                                                                                                                                                            Set<CharSequence> out = new HashSet<CharSequence>();
                                                                                                                                                                                                            int length = input.length();

                                                                                                                                                                                                            for (int i = 1; i < length - 1; i++)
                                                                                                                                                                                                            {
                                                                                                                                                                                                            for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++)
                                                                                                                                                                                                            {
                                                                                                                                                                                                            if (input.charAt(j) == input.charAt(k)) {
                                                                                                                                                                                                            out.add(input.subSequence(j, k + 1));
                                                                                                                                                                                                            } else {
                                                                                                                                                                                                            break;
                                                                                                                                                                                                            }
                                                                                                                                                                                                            }
                                                                                                                                                                                                            }
                                                                                                                                                                                                            return out;
                                                                                                                                                                                                            }
                                                                                                                                                                                                            }

                                                                                                                                                                                                            **Get All Palindrome in s given string**


                                                                                                                                                                                                            Output
                                                                                                                                                                                                            D:Java>java GetAllPalindromes
                                                                                                                                                                                                            Enter a string



                                                                                                                                                                                                            Hello user nitin is my best friend wow !



                                                                                                                                                                                                            Answer is set is [nitin, nitin , wow , wow, iti]



                                                                                                                                                                                                            D:Java>






                                                                                                                                                                                                            share|improve this answer


























                                                                                                                                                                                                              0














                                                                                                                                                                                                              enter image description here



                                                                                                                                                                                                              import java.util.Collections;
                                                                                                                                                                                                              import java.util.HashSet;
                                                                                                                                                                                                              import java.util.Scanner;
                                                                                                                                                                                                              import java.util.Set;

                                                                                                                                                                                                              public class GetAllPalindromes
                                                                                                                                                                                                              {
                                                                                                                                                                                                              static Scanner in;

                                                                                                                                                                                                              public static void main(String args)
                                                                                                                                                                                                              {
                                                                                                                                                                                                              in = new Scanner(System.in);
                                                                                                                                                                                                              System.out.println("Enter a string n");
                                                                                                                                                                                                              String abc = in.nextLine();
                                                                                                                                                                                                              Set a = printAllPalindromes(abc);
                                                                                                                                                                                                              System.out.println("set is " + a);
                                                                                                                                                                                                              }

                                                                                                                                                                                                              public static Set<CharSequence> printAllPalindromes(String input)
                                                                                                                                                                                                              {
                                                                                                                                                                                                              if (input.length() <= 2) {
                                                                                                                                                                                                              return Collections.emptySet();
                                                                                                                                                                                                              }

                                                                                                                                                                                                              Set<CharSequence> out = new HashSet<CharSequence>();
                                                                                                                                                                                                              int length = input.length();

                                                                                                                                                                                                              for (int i = 1; i < length - 1; i++)
                                                                                                                                                                                                              {
                                                                                                                                                                                                              for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++)
                                                                                                                                                                                                              {
                                                                                                                                                                                                              if (input.charAt(j) == input.charAt(k)) {
                                                                                                                                                                                                              out.add(input.subSequence(j, k + 1));
                                                                                                                                                                                                              } else {
                                                                                                                                                                                                              break;
                                                                                                                                                                                                              }
                                                                                                                                                                                                              }
                                                                                                                                                                                                              }
                                                                                                                                                                                                              return out;
                                                                                                                                                                                                              }
                                                                                                                                                                                                              }

                                                                                                                                                                                                              **Get All Palindrome in s given string**


                                                                                                                                                                                                              Output
                                                                                                                                                                                                              D:Java>java GetAllPalindromes
                                                                                                                                                                                                              Enter a string



                                                                                                                                                                                                              Hello user nitin is my best friend wow !



                                                                                                                                                                                                              Answer is set is [nitin, nitin , wow , wow, iti]



                                                                                                                                                                                                              D:Java>






                                                                                                                                                                                                              share|improve this answer
























                                                                                                                                                                                                                0












                                                                                                                                                                                                                0








                                                                                                                                                                                                                0






                                                                                                                                                                                                                enter image description here



                                                                                                                                                                                                                import java.util.Collections;
                                                                                                                                                                                                                import java.util.HashSet;
                                                                                                                                                                                                                import java.util.Scanner;
                                                                                                                                                                                                                import java.util.Set;

                                                                                                                                                                                                                public class GetAllPalindromes
                                                                                                                                                                                                                {
                                                                                                                                                                                                                static Scanner in;

                                                                                                                                                                                                                public static void main(String args)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                in = new Scanner(System.in);
                                                                                                                                                                                                                System.out.println("Enter a string n");
                                                                                                                                                                                                                String abc = in.nextLine();
                                                                                                                                                                                                                Set a = printAllPalindromes(abc);
                                                                                                                                                                                                                System.out.println("set is " + a);
                                                                                                                                                                                                                }

                                                                                                                                                                                                                public static Set<CharSequence> printAllPalindromes(String input)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                if (input.length() <= 2) {
                                                                                                                                                                                                                return Collections.emptySet();
                                                                                                                                                                                                                }

                                                                                                                                                                                                                Set<CharSequence> out = new HashSet<CharSequence>();
                                                                                                                                                                                                                int length = input.length();

                                                                                                                                                                                                                for (int i = 1; i < length - 1; i++)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                if (input.charAt(j) == input.charAt(k)) {
                                                                                                                                                                                                                out.add(input.subSequence(j, k + 1));
                                                                                                                                                                                                                } else {
                                                                                                                                                                                                                break;
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }
                                                                                                                                                                                                                return out;
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }

                                                                                                                                                                                                                **Get All Palindrome in s given string**


                                                                                                                                                                                                                Output
                                                                                                                                                                                                                D:Java>java GetAllPalindromes
                                                                                                                                                                                                                Enter a string



                                                                                                                                                                                                                Hello user nitin is my best friend wow !



                                                                                                                                                                                                                Answer is set is [nitin, nitin , wow , wow, iti]



                                                                                                                                                                                                                D:Java>






                                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                                enter image description here



                                                                                                                                                                                                                import java.util.Collections;
                                                                                                                                                                                                                import java.util.HashSet;
                                                                                                                                                                                                                import java.util.Scanner;
                                                                                                                                                                                                                import java.util.Set;

                                                                                                                                                                                                                public class GetAllPalindromes
                                                                                                                                                                                                                {
                                                                                                                                                                                                                static Scanner in;

                                                                                                                                                                                                                public static void main(String args)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                in = new Scanner(System.in);
                                                                                                                                                                                                                System.out.println("Enter a string n");
                                                                                                                                                                                                                String abc = in.nextLine();
                                                                                                                                                                                                                Set a = printAllPalindromes(abc);
                                                                                                                                                                                                                System.out.println("set is " + a);
                                                                                                                                                                                                                }

                                                                                                                                                                                                                public static Set<CharSequence> printAllPalindromes(String input)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                if (input.length() <= 2) {
                                                                                                                                                                                                                return Collections.emptySet();
                                                                                                                                                                                                                }

                                                                                                                                                                                                                Set<CharSequence> out = new HashSet<CharSequence>();
                                                                                                                                                                                                                int length = input.length();

                                                                                                                                                                                                                for (int i = 1; i < length - 1; i++)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                if (input.charAt(j) == input.charAt(k)) {
                                                                                                                                                                                                                out.add(input.subSequence(j, k + 1));
                                                                                                                                                                                                                } else {
                                                                                                                                                                                                                break;
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }
                                                                                                                                                                                                                return out;
                                                                                                                                                                                                                }
                                                                                                                                                                                                                }

                                                                                                                                                                                                                **Get All Palindrome in s given string**


                                                                                                                                                                                                                Output
                                                                                                                                                                                                                D:Java>java GetAllPalindromes
                                                                                                                                                                                                                Enter a string



                                                                                                                                                                                                                Hello user nitin is my best friend wow !



                                                                                                                                                                                                                Answer is set is [nitin, nitin , wow , wow, iti]



                                                                                                                                                                                                                D:Java>







                                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                                                answered Jun 12 '17 at 9:15









                                                                                                                                                                                                                Keshav Gera

                                                                                                                                                                                                                2,4031727




                                                                                                                                                                                                                2,4031727






















                                                                                                                                                                                                                    1 2
                                                                                                                                                                                                                    next




                                                                                                                                                                                                                    protected by Community Apr 7 '13 at 17:01



                                                                                                                                                                                                                    Thank you for your interest in this question.
                                                                                                                                                                                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                                                                                    Would you like to answer one of these unanswered questions instead?



                                                                                                                                                                                                                    Popular posts from this blog

                                                                                                                                                                                                                    404 Error Contact Form 7 ajax form submitting

                                                                                                                                                                                                                    How to know if a Active Directory user can login interactively

                                                                                                                                                                                                                    Refactoring coordinates for Minecraft Pi buildings written in Python