- /*
- * 判断一个字符串是否是对称字符串
- * 例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
- */
- public class Test1 {
- public static void main(String[] args) {
-
- String str = null;
- System.out.println(str + ":" + isSymmetric(str));
- str = "";
- System.out.println(str + ":" + isSymmetric(str));
- // 偶数长度
- str = "abccba";
- System.out.println(str + ":" + isSymmetric(str));
- // 奇数长度
- str = "abcdcba";
- System.out.println(str + ":" + isSymmetric(str));
- str = "abca";
- System.out.println(str + ":" + isSymmetric(str));
- }
- public static boolean isSymmetric(String str) {
- if (null == str) {
- return false;
- }
- for (int i = 0; i < str.length() / 2; i++) {
- // 比较距字符串两头长度相同的字符是否一样
- if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
- return false;
- }
- }
- return true;
- }
- }
复制代码
|
|