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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 18:51   94   0

/*
* 判断一个字符串是否是对称字符串
例如"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("该字符串是对称字符串");
 }

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP