宗策

导航

charAt()检测回文

package seday01;

/**
* char charAt(int index) 返回指定位置对应的字符
* @author xingsir
*/
public class CharAtDemo {

public static void main(String[] args) {

//                  0123456789012345(此为字符串下边位置)
String str = "thinking in java";
char c = str.charAt(2);
System.out.print(c);

/*
* 检测回文
*/
// 0123 5678 (此为字符串下边)
String line = "山东落花生花落东山";
for (int i = 0; i < line.length() / 2; i++) {
char c1 = line.charAt(i);
char c2 = line.charAt(line.length() - 1 - i);
if (c1 != c2) {
System.out.print("不");
break;
}

}
System.out.print("是回文");
}

}

posted on 2019-11-28 11:01  宗策  阅读(214)  评论(0编辑  收藏  举报