Check string for palindrome

前端 未结 30 3012
悲哀的现实
悲哀的现实 2020-11-22 02:47

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 th

相关标签:
30条回答
  • 2020-11-22 03:26

    Why not just :

    boolean isPalindrom(String s) {
            char[] myChars = s.toCharArray();
            for (int i = 0; i < myChars.length/2; i++) {
                if (myChars[i] != myChars[myChars.length - 1 - i]) {
                    return false;
                }
            }
            return true;
    }
    
    0 讨论(0)
  • 2020-11-22 03:26

    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);
    }
    }
    
    0 讨论(0)
  • 2020-11-22 03:27

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

    }

    0 讨论(0)
  • 2020-11-22 03:28

    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.
    0 讨论(0)
  • 2020-11-22 03:29

    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!

    0 讨论(0)
  • 2020-11-22 03:29
    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;
        }
    
    0 讨论(0)
提交回复
热议问题