3、判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
- public class Test3 {
- public static void main(String[] args)
- {
- String s="abdcdba"; //定义一个字符串
- show(s);
-
- }
- public static String reverseString(String s)
- {
- char[] chs = s.toCharArray();
- reverse(chs);
- return new String(chs);
- }
- private static void reverse(char[] arr)
- {
- for (int start =0,end=arr.length-1;start<end ;start++,end-- )
- {
- swap(arr,start,end);
- }
- }
- private static void swap(char[] arr,int x,int y)
- {
- char temp = arr[x];
- arr[x] = arr[y];
- arr[y] = temp;
- }
- public static void show(String s)
- {
-
- if (s.equals(reverseString(s)))
- System.out.println("对称");
- else if(s.length()%2!=1)
- System.out.println("字符串有问题");
- else
- System.out.println("不对称");
- }
- }
复制代码
|