With a recursive call to determine whether a string is a palindrome

Ideas: first determine the length of the string, if the string is a palindrome is, if more than one then, calls the function determination. Whether it is a palindrome output via the return value.

The function is passed the array of strings there are mainly two parameters, as a subscript, performed by two parameters CharAt function strings of the corresponding part of the string comparison, for the last two parameters, plus one behind the front of minus 1, and gradually closer to the center. Sequentially comparing, when subjected to only one or two additional analysis time.

The main code is as follows:

Package Chengzhang;
 Import java.util.Scanner;
 public  class Jiecheng 
{ 
    public  static  int diaoyong (C String, int A, int B) 
    { 
        IF (A == B) // at the time when odd 
            return . 1 ;
         IF (A = +. 1 B =) // when it is an even number and only when the two 
        {
             iF (c.charAt (a) == c.charAt (B)) // for comparing both sides of the 
                return . 1 ;
             the else 
                return -1 ; 
        } 
        the else 
        {
            if(c.charAt(a)==c.charAt(b))
            {
                a+=1;
                b-=1;
                return diaoyong(c,a,b);//递归调用
            }
            else
                return -1;
        }
            
    }

    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        String wen=sc.nextLine();

            int w1=wen.length()-1;
            if(w1==0)// only when a character 
                System.out.println ( "palindromic" );
             the else 
            { 
                IF (diaoyong (Wen, 0, W1) ==. 1 ) 
                    System.out.println ( "palindromic" );
                 the else  IF (diaoyong (Wen, 0, W1) == -. 1 ) 
                    System.out.println ( "not a palindrome" ); 
            } 
    } 

}

It is important recursive call, which function to call itself. The last important point is CharAt string function application which was applied in accordance with the style and compare the array of characters.

Guess you like

Origin www.cnblogs.com/dazhi151/p/11577695.html