(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