題目
Problem Description
“迴文串”是一個正讀和反讀都一樣的字符串,比如“level”或者“noon”等等就是迴文串。請寫一個程序判斷讀入的字符串是否是“迴文”。
Input
輸入包含多個測試實例,輸入數據的第一行是一個正整數n,表示測試實例的個數,後面緊跟着是n個字符串。
Output
如果一個字符串是迴文串,則輸出”yes”,否則輸出”no”.
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
###思路
比較原字符串與倒置取反的字符串是否相等
代碼
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++){
String s=sc.next();
StringBuffer buf=new StringBuffer();
buf.append(s);
String str=buf.reverse().toString();
if(s.equals(str))
System.out.println("yes");
else
System.out.println("no");
}
}
}
}