Workshop to determine whether a string is a palindrome

I, entitled: determining whether the input character string is a palindrome series.

Second, the basic idea of ​​the program:

1 first determines if the length of the string is 0 or 1, if so, this string is palindromic, the routine ends directly

2. If the string length is greater than 1, the first string is converted into an array of characters, custom banner In Flag, head and tail identification character array, the character array and the first address passed to a recursive method as defined above

3. The head and tail mark with an array of head and tail characters one by one, and if consistent, recursively calls itself recursively until a termination condition. Should a match does not have to return the output string is not a palindrome.

Recursive termination condition for the head and tail mark is equal or differ by 1, return output string is palindromic.

III. Source Codes

 

 1 import java.util.Scanner;
 2 
 3 public class panduanhui {
 4      static char []a=new char[1000];
 5      static String str;
 6      static int flag=0;
 7      static int i=0;
 8      static int k=0;
 9     public static void main(String[] args) {
10       Scanner  input=new Scanner(System.in);
11       System.out.println ( "Enter a string:" );
 12 is         STR = input.nextLine ();
 13 is       // string into a character array 
14         A = str.toCharArray ();
 15         K = A. length;
 16         int FLAG1 = 0 ;
 . 17         IF (a.length a.length == == 0 ||. 1 ) 
 18 is         {
 . 19             System.out.println ( "string" + str + "palindromic sequence" );  
 20         }
 21 is         the else {
 22 is         huiwenpanduan (A, I, K);
 23 is         FLAG1 =. 1 ;
 24         }
 25        IF (FLAG1 ==. 1 )
 26 is         {
 27          // determination output 
28         IF (In Flag == 0 ) {
 29             System.out.println ( "string" + str + "is not a palindrome" );
 30         }
 31 is         the else {
 32             System.out.println ( "string" + str + "palindromic sequence" );
 33         }
 34         }
 35  }
 36      // character array as a parameter in the method 
37 [      public  static  void huiwenpanduan ( char [] A, int I, int K) {
 38 is         // last characters comparator 
39          IF (A [I] == A [-K. 1 ])
 40          {
 41 is              // marked advance 
42 is              I ++ ;
 43 is              // trailing mark shift 
44 is              K-- ;
 45              // recursive termination condition 
46              IF (Ki Ki ||. 1 == == 2 ) {  
 47                  In Flag =. 1 ;
 48                  return ;
 49              }
 50          // recursive call itself 
51 is          huiwenpanduan (A, I, K);
 52 is          }
 53 is          // ending characters are different, is not a palindromic sequence, return 
54         else {
55             flag=0;
56             return ;
57         }     
58     }
59         
60     }

Fourth, the results:

 

 

 

 

V. Summary

1. To clear the recursive recursive termination condition

2. To call itself recursively

3. Each call recursive method should reduce the scale of the problem

 

Guess you like

Origin www.cnblogs.com/yang2000/p/11575561.html