判断一个字符串是否是对称字符串

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

 */
/*
 * 知识储备
 * public final char charAt(int index)读取给定索引处的字符。
 * 将第i个与第(字符串长度-1-i)处的字符串一一进行比较
 */

import java.util.Scanner;
public class Test3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		int count = 0;
		for(int i = 0;i < str.length() / 2;i++) {
			if(str.charAt(i) == str.charAt(str.length() - 1 -i)) {
				count++;
			}else {
				System.out.println("该字符串不是对称字符串");
				break;
			}
		}
		if(count == str.length() / 2)
			System.out.println("该字符串是对称字符串");
	}

}

猜你喜欢

转载自blog.csdn.net/dianmomanxue/article/details/81299894