September 23 judgment palindrome

First, the title: a string to determine whether the input is a palindrome

Second, programming ideas

Defines a boolean functions, and uses a recursive algorithm. Functions provided in the three parameters, namely the input of the incoming string, an integer type variable start, an integer type variable length, in vivo use of the function if else statement to determine whether the string is passed in a special case,

By toCharArray () function string into an array of characters, the last one by comparing the head and tail of the string, and returns the result.

Third, the source code

import java.util.Scanner;
public class palindrome {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String c=scanner.next();
        boolean flag = find(c,0,c.length());
        System.out.println(flag);
    }
//定义三个参数
private static boolean find(String c, int start, int length) { if(length<=1) return true; else if(c.toCharArray()[start]==c.toCharArray()[length-1]){ return find(c,start+1,length-1); } return false; } }

Fourth, the test:

 

 

 

 

 

V. Conclusions summary

Using a recursive algorithm can reduce the amount of code and convenient.

The relationship between the character string and flexible conversion can be achieved specific issues.

Analyze problems of scale and detail, one by one to solve.

 

Guess you like

Origin www.cnblogs.com/a155-/p/11582929.html