Check string for palindrome

前端 未结 30 3693
悲哀的现实
悲哀的现实 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: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;
    }
    

    }

提交回复
热议问题