1 package demo_2015_11_18;
2
3 import java.util.Arrays;
4 import java.util.Scanner;
5
6 public class DEMO {
7
8 /**
9 * 判断输入的内容是否是回文
10 * @param args
11 */
12 public static void main(String[] args) {
13 Scanner s = null;
14 try {
15 //读入数据源
16 s = new Scanner(System.in);
17 String srcStr = s.nextLine();
18 char[] srcChars = srcStr.toCharArray();
19
20 //判断数组长度奇偶
21 char[] t1 = null;
22 char[] t2 = null;
23 //分割数组
24 t1 = Arrays.copyOfRange(srcChars, 0, srcChars.length/2);
25 if(srcChars.length%2 == 0){
26 //偶数
27 t2 = Arrays.copyOfRange(srcChars, srcChars.length/2, srcChars.length);
28 }else{
29 //奇数
30 t2 = Arrays.copyOfRange(srcChars, srcChars.length/2 + 1, srcChars.length);
31 }
32
33 //反转后半截数组
34 char[] temp = new char[t2.length];
35 for (int i = t2.length - 1; i >= 0; i--) {
36 temp[t2.length - 1 - i] = t2[i];
37 }
38
39 System.out.println(Arrays.equals(t1,temp));
40
41 } catch (Exception e) {
42 s.close();
43 e.printStackTrace();
44 }
45
46 }
47
48 }
49
结果展示:
asddsa
true
asdsasd
false
asdasd
false
asdxdsa
true