Check string for palindrome

前端 未结 30 3010
悲哀的现实
悲哀的现实 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:33

    public class Palindromes {
        public static void main(String[] args) {
             String word = "reliefpfpfeiller";
             char[] warray = word.toCharArray(); 
             System.out.println(isPalindrome(warray));       
        }
    
        public static boolean isPalindrome(char[] word){
            if(word.length%2 == 0){
                for(int i = 0; i < word.length/2-1; i++){
                    if(word[i] != word[word.length-i-1]){
                        return false;
                    }
                }
            }else{
                for(int i = 0; i < (word.length-1)/2-1; i++){
                    if(word[i] != word[word.length-i-1]){
                        return false;
                    }
                }
            }
            return true;
        }
    }
    

提交回复
热议问题