Recursive method for judging palindrome

Source: Internet
Author: User

(1) Using recursive method to determine whether a string is a palindrome ( palindrome);

" A palindrome is the same sentence that is read and read in reverse. Like , "Who am I?"

The algorithm for detecting palindrome using recursive algorithm is described as follows:

A single or Zero-character string is a palindrome.

Any other string is a palindrome if the first and last characters is the same, and the string that remains, excepting tho Se characters, is a palindrome.

1  Packagerecursive text;2 ImportJava.util.*;3  Public classhuiwen{4     StaticScanner x=NewScanner (system.in);5     Static CharStrs[] =New Char[1000];6     Static intJ=0;7      Public Static voidMain (string[] args) {8String s = x.next ();//input from keyboard9          for(intI=0;i<s.length (); i++) {//convert to character arrayTenSTRS[J] =S.charat (i); OneJ + +; A         } -         BooleanHuiwen = Ishuiwen (STRs, 0, J-1, j); - System.out.println (Huiwen); the     } -      Public Static BooleanIshuiwen (CharA[],intLowintHighintlength) { -         if(length = = 1 | | length = = 0) -             return true;//The number of characters is 1, must be palindrome +         if(A[low]! = A[high] | | low >= high) {//the first character is compared to the last character -             return false; +         } A         returnIshuiwen (A, low + 1, high-1,length-2);//Recursive at     } -}

Run:

Recursive method for judging palindrome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.