黑马程序员----判断一个字符串是否是对称字符串

本文提供了一个简单的Java程序示例,用于判断一个字符串是否为对称字符串(回文)。通过对字符串两端的字符进行对比,该程序能有效检测出对称字符串,并针对不同长度的字符串进行了测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------

public class Test02 {


/**
* @param args
*/
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 = "abcdba";
System.out.println("“" +str + "”" + isSymmetric(str));


// 奇数长度
str = "abcdcba";
System.out.println("“" +str + "”" + isSymmetric(str));


str = "abca";
System.out.println("“" +str + "”" + isSymmetric(str));
}


public static String isSymmetric(String str) {
if (null == str) {
return "字符串为空";
}
for (int i = 0; i < str.length() / 2; i++) {
// 比较距字符串两头长度相同的字符是否一样
if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
return "不是对称字符串";
}
}
return "是对称字符串";
}
}

---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值