Recursive solve a string palindrome judgment

Thinking

1. First, the definition of a string, and initializes.

2. Set a function, compared to the beginning and end of the string, if the same, then recursively, on the second and penultimate strings are compared, until complete comparison, output true; if not identical, to output false .

3. At the end recursion condition that is to compare the first character

30 minutes debug the code for

Source

package sizeyunsuan;
import java.util.Scanner;
public class Huiwen
{
  public static void main(String [] args)
  {
   System.out.print("Please input a string:");
   Scanner sca=new Scanner(System.in);
   String str=sca.nextLine();
   System.out.println(str+"is a Huiwen.true or false?");
   int t=str.length()-1;
   if((t+1)==0)
    System.out.println("true");
   int n=0;
   complete(n,str,t);
  }
  public static void complete(int n,String str,int t)
  {
   int s=t+1;
   if(s==1)
    System.out.println("true");
   if(str.charAt(n)==str.charAt(t))
    complete(++n,str,--t);
   else
    System.out.println("false");
  
  }
}
   

Test shots:

 

 

 

Harvest summary:

1. Understand the principles of recursion, we can better use recursion to solve the problem.

2. Know his place on the programming deficiencies, and make plans to solve.

 

Guess you like

Origin www.cnblogs.com/2210633591zhang/p/11586704.html