package programList;
import java.util.Scanner;
public class P5_14判斷回文串 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string:");
String s = input.nextLine();
int low = 0;
int high = s.length() - 1;
boolean isPalindrome = true;
while(low < high) {
if(s.charAt(low) != s.charAt(high)) {
isPalindrome = false;
break;
}
low++;
high--;
}
if(isPalindrome){
System.out.println(s + " is a palindrome");
}else{
System.out.println(s + " is not a palindrome");
}
}
}
判斷輸入字符串是否對稱吧.
比如abba 就是true
abbcba就會是false
if(s.charAt(low) != s.charAt(high)) {//s.charAt(low)意思是從第一位開始拿字符, s.charAt(high)意思是拿到最後一位字符
isPalindrome = false;
break;
}