Palindrome recursive quiz summary

Title: recursive way to determine whether a string is a palindrome (palindrome)

Design: to determine whether a string is a palindrome, we must first determine the character and the last character are equal, then determine the second and second last character are equal and so on, so to use recursion, first to the end of the recursive determination condition, and of much smaller, a corresponding character sequence comparison for equality. If the situation does not appear equal, the string is not a palindrome.

package palindrome;

import java.util.Scanner;

public class str {
	public static void  pd(String a,int n)
	{
	     if(a.charAt(a.length()-n)==a.charAt(n-1))
			{
				if(2*n-1-a.length()==0||2*n-1-a.length()==1)
				System.out.println("true"); 
				else  pd(a,n-1);
			}
	     else System.out.println("false"); 
		
	}

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.println("请输入字符串:");
		String a=in.next();
		int n=a.length();
		pd(a, n);
	}
}

  

 

 

 Programming summary analysis: recursive programming the first end of the sentence is determined, for example, the title of this character at the corresponding conditions are equal too, if the subtraction finally obtain 1 or 0, the completion string is determined to be ended, each recursive n to reduce the number 1, to reduce the judgment.

Guess you like

Origin www.cnblogs.com/zwx655/p/11579614.html