java回文串

  用递归写函数判断回文串,当长度为1或0时输出是回文串结束,当长度大于1时开始递归。

 1 package Zcf;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Statistics {
 6     public static void main(String[] args) {
 7     System.out.println("请输入字符串:");
 8     Scanner in = new Scanner(System.in);
 9     String s=in.next();
10     cuan(s,0);
11     }
12     public static void cuan (String a,int i) {
13         //String ss=a;
14         int len=a.length();
15         len-=i;
16         if(len==1||len==0)
17         {
18             System.out.println("是回文串!");
19             return ;
20         }
21         else
22         {
23             char q=a.charAt(i);
24             char w=a.charAt(len-1);
25             if(q==w)
26             {
27                 cuan(a,++i);
28             }
29             else
30             {
31                 System.out.println("不是回文串!");
32                 return ;
33             }
34             
35         }
36     }
37 }
原文地址:https://www.cnblogs.com/125418a/p/11586760.html