題目連結
Palindromes _easy version
Problem Description
“回文串”是一個正讀和反讀都一樣的字元串,比如“level”或者“noon”等等就是回文串。請寫一個程式判斷讀入的字元串是否是“回文”。
Input
輸入包含多個測試執行個體,輸入資料的第一行是一個正整數n,表示測試執行個體的個數,後面緊跟着是n個字元串。
Output
如果一個字元串是回文串,則輸出"yes",否則輸出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
思路
兩個思路,
一個是先得到字元串長度len,i循環到len/2;
比較string[i]與 string[len-1-i]是否相同;
另一個就比較猥瑣了,庫函數strrev()可以倒置字元串,哈哈哈哈
代碼
#include <stdio.h>
#include <string.h>
int main(void)
{
int n;
char s[1024];
char t[1024];
scanf("%d%*c", &n);
while (n--)
{
gets(s);
strcpy(t, s);//複制
strrev(s);//倒置
puts(strcmp(t, s) ? "no" : "yes");
}
return 0;
}