package com.itheima;
import java.util.Scanner;
/*
* 3、判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
*/
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();//输入要判断的字符串
char[] ch = str.toCharArray();//把字符串变成字符数组
// byte[] by = str.getBytes();
int k = 0, l = 0;
for (int i = 0, j = ch.length - 1; i < ch.length / 2; i++, j--) {
if (ch == ch[j]) {//比较两边字符是否相等
k++;
} else {
l++;
}
}
if (k != 0 & l == 0) {
System.out.println("是对称字符串");//输出结果
} else {
System.out.println("不是对称字符串");
}
}
}
大家看下有什么可以改进的 |
|