Java 实例 使用递归检查回文字符串


import java.util.Scanner;
class PalindromeCheck
    //My Method to check
    public static boolean isPal(String s)
    {   // if length is 0 or 1 then String is palindrome
        if(s.length() == 0 || s.length() == 1)
            return true; 
        if(s.charAt(0) == s.charAt(s.length()-1))
        /* check for first and last char of String:
         * if they are same then do the same thing for a substring
         * with first and last char removed. and carry on this
         * until you string completes or condition fails
         * Function calling itself: Recursion
        return isPal(s.substring(1, s.length()-1));

        /* If program control reaches to this statement it means
         * the String is not palindrome hence return false.
        return false;

    public static void main(String[]args)
        //For capturing user input
        Scanner scanner = new Scanner(;
        System.out.println("Enter the String for check:");
        String string = scanner.nextLine();
        /* If function returns true then the string is
         * palindrome else not
            System.out.println(string + " is a palindrome");
            System.out.println(string + " is not a palindrome");


Enter the String for check:
qqaabb is not a palindrome

输出 2:

Enter the String for check:
cocoococ is a palindrome

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Java 实例