?? "回文"是指順讀和反讀內容都相同的字符串,如:"ABCCBA".這里引入兩個指針變量,開始時,
分別指向字符串的首末字符,當兩個指針所指字符相等時,兩指針分別向前向后移一個字符位置,
并繼續比較,直到兩指針相遇.則說明該字符串是回文.若比較過程中,發現兩字符不相等,則
可以判斷該字符串不是回文.
代碼如下:
/*判斷字符串是否是回文*/
#include<stdio.h>
#define MAX 50
int cycle(char *s)
{
? char *h,*t;
? for(h=s,t=s+strlen(s)-1;t>h;h++,t--)
? {
??? if(*h!=*t)
??? {
??????? printf("%c",h);
??????? break;
??? }
? }
? return t<=h;
}
main()
{
? char s[MAX];
? while(1)
? {
??? puts("Please input the string you want to judge(input ^ to quit):");
??? scanf("%s",s);
??? if(s[0]=='^')
????? break;
??? if(cycle(s))
??? printf("%s is a cycle string.\n",s);
??? else
??? printf("%s is not a cycle string.\n",s);
? }
? puts("\nThank you for you using ,bye bye!\n");
}
輸出結果:
Please input the string you want to judge(input ^ to quit):
abcabc
?abcabc is not a cycle string.
Please input the string you want to judge(input ^ to quit):
abccba
abccba is a cycle string.
Please input the string you want to judge(input ^ to quit):
?
?
posted on 2006-11-05 16:58
matthew 閱讀(4133)
評論(1) 編輯 收藏 所屬分類:
數據結構與算法設計