【问题描述】
判断字符串是否回文或者“镜像”.
【解决过程】
代码如下:
13746254 | 401 | Palindromes | Accepted | ANSI C | 0.018 | 2014-06-12 21:52:43 |
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
#ifdef LOCAL
freopen("filein.txt","r",stdin);
freopen("fileout.txt","w",stdout);
#endif
const char rel[26]="A 3 HIL JM O 2TUVWXY5",ren[10]="1SE Z 8 ";
char s[25];
int i,is_mir=1,is_pal=1;
while(scanf("%s",s)==1){
for(i=0;i<strlen(s);i++)
if(isdigit(s[i]))
{if(ren[s[i]-'1']!=s[strlen(s)-1-i]) {is_mir=0;break;}}
else
{if(rel[s[i]-'A']!=s[strlen(s)-1-i]) {is_mir=0;break;}}
for(i=0;i<strlen(s);i++)
if(s[i]!=s[strlen(s)-1-i]) {is_pal=0;break;}
if(!is_pal&&!is_mir) printf("%s -- is not a palindrome.\n\n",s);
if(is_pal&&!is_mir) printf("%s -- is a regular palindrome.\n\n",s);
if(!is_pal&&is_mir) printf("%s -- is a mirrored string.\n\n",s);
if(is_pal&&is_mir) printf("%s -- is a mirrored palindrome.\n\n",s);
is_mir=1;is_pal=1;
};
return 0;
}
【教训】
1、看题要仔细!