A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Gqg 中级黑马   /  2016-4-1 23:03  /  573 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串

2 个回复

倒序浏览
转为StringBuffer类型,用他的reverse()反转方法
回复 使用道具 举报
        String[] s = {"aba","3223","abccba","7890987","123123"};
                String s1 = "";
               
                for(int i=0;i<5;i++) {                      //第一层for循环把字符串数组转换成字符串
                         s1 = s;
                         char[] c2 =new char[s1.length()];
                        //System.out.println(s1);
                         for(int j=0;j<s1.length();j++) {      // 这层for循环把字符串转换成字符数组
                                char c = s1.charAt(j);
                                c2[j] = c;
                                //System.out.print(c + " ");
                        }
                        for(int k=0;k<c2.length/2;k++) {      // c2.length/2是判断的次数,
                                if(c2[k] == c2[c2.length-1-k]) {
                                        if(k==c2.length/2-1) {
                                                print(c2);
                                                System.out.println("是对称的");
                                        }
                                }else {
                                        print(c2);
                                        System.out.println("不是对称的");
                                        break;
                                }
                        }
                }
        }
        public static void print(char[] arr) {            //遍历字符数组
                for(int i=0;i<arr.length;i++) {
                        System.out.print(arr);
                }
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马