使用回调函数实现回文判断

面试时遇到一个问题,给定一个字符串,通过一定方法完成回文判断,并且必须要用到回调函数。

 按照思路,如下实现

  

public static void main(String[] args) {
        if(isPalindrome("abc")){
            System.err.println("是回文");
        }else{
            System.out.println("不是回文");
        }
    }
    static boolean isPalindrome(String str){
        if(str==""){
            return false;
        }else{
            if("yes".equals(checked(str))){
                return true;
            }else{
                return false;
            }
        }
    }
    static String checked(String str){
          
           if(str==null||str==""){
               return "yes";
           }else{
               char cha[]=new char[str.length()];
               
               for(int i=0;i<str.length();i++){
     
                   cha[i]= str.charAt(i);
               }
               String f="";
               if(cha[0]==cha[cha.length-1]){
                  
                   for(int j=1;j<cha.length-1;j++){
                       f+=cha[j];
                   }
                   return checked(f);
               }else{
                   
                   return "no";
               }
           }
       }
    
原文地址:https://www.cnblogs.com/wangzun/p/6703877.html