您的位置:首页 > 其它

UVa 401 Palindromes

2016-11-27 11:52 369 查看
//#include <iostream>
//#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
//using namespace std;
const char s[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";

//if the string is a palindrome
rev(char string[], char reved[]) {
int len = strlen(string);
int i;
for(i = 0; i < len; i++) {
reved[len - i - 1] = string[i];
}
}

//if the string is a mirrored string
mir(char string[], char mired[]) {
int len = strlen(string);
strcpy(mired, string);
int i;
for(i = 0; i < len; i++) {
int index;
if(isalpha(mired[i])) index = mired[i] - 'A';
else if(isdigit(mired[i])) index = mired[i] - '0' + 25;
if(s[index]) mired[i] = s[index];
}
}

int main() {
//ios::sync_with_stdio(false);
//FILE *f = fopen("in.txt", "r");

char input[30];

int palindrome;
int mirrored;

char reved[30];
char mirtmp[30];
char mired[30];

while(scanf("%s", input) == 1) {
memset(reved, 0, sizeof(reved));
memset(mired, 0, sizeof(mired));

rev(input, reved);
mir(input, mirtmp);
rev(mirtmp, mired);

palindrome = !strcmp(input, reved);
mirrored = !strcmp(input, mired);

//printf("%s %s %s\n", reved, mired, input);

if(mirrored && palindrome) printf("%s -- is a mirrored palindrome.", input);
else if(mirrored) printf("%s -- is a mirrored string.", input);
else if(palindrome) printf("%s -- is a regular palindrome.", input);
else printf("%s -- is not a palindrome.", input);
printf("\n\n");
}

//fclose(f);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: