Recursive way to determine whether a string is a palindrome (palindrome).
1. Design ideas
In determining whether a string is a palindrome, the recursive method, we must first analyze what to do is repeat, repeat here to judge character is not equal at both ends, or until the last remaining 0 characters when .
2. source code
package eg1;
import java.util.*;
public class eg1 {
public static void main(String[] args)
{
System.out.print("请输入字符串:");
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
boolean flag = find(s,0,s.length());
System.out.println(flag);
}
private static boolean find(String str, int start, int length) {
if(length<=1)
return true;
else if(str.toCharArray()[start]==str.toCharArray()[length-1]){
return find(str,start+1,length-1);
}
return false;
}
}
3. Run the result screenshots
4. Programming summary analysis
Intermediate character when determination is to be noted that the number of characters palindromic single number / case where only one character / null character.