思路:按照题目描述,进行三种情况的模拟,先对输入的字符串进行第三种情况的判断,如果不是第三种,再分别判断后两者,如果都不是,再输出no;(注意的是字符串在函数中的处理情况,不注意会导致函数的实参变化,从而导致错误);
代码如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int pal(char *a)
{
int i;
int len;
int x;
char b[30];
len = strlen(a);
x = 0;
for (i = len - 1; i >= 0; i--)
{
b[x++] = a[i];
}
b[x] = '\0';
if (!strcmp(a, b))
{
return 1;
}
else
return 0;
}
int mir(char *b)
{
int i;
int len;
int flag;
char a[30];
char temp[30];
int x;
len = strlen(b);
flag = 1;
x = 0;
strcpy(a, b);
for (i = 0; i < len; i++)
{
if (a[i] == 'A')
a[i] = 'A';
else if (a[i] == 'E')
a[i] = '3';
else if (a[i] == 'H')
a[i] = 'H';
else if (a[i] == 'I')
a[i] = 'I';
else if (a[i] == 'J')
a[i] = 'L';
else if (a[i] == 'L')
a[i] = 'J';
else if (a[i] == 'M')
a[i] = 'M';
else if (a[i] == 'O')
a[i] = 'O';
else if (a[i] == 'S')
a[i] = '2';
else if (a[i] == 'T')
a[i] = 'T';
else if (a[i] == 'U')
a[i] = 'U';
else if (a[i] == 'V')
a[i] = 'V';
else if (a[i] == 'W')
a[i] = 'W';
else if (a[i] == 'X')
a[i] = 'X';
else if (a[i] == 'Y')
a[i] = 'Y';
else if (a[i] == 'Z')
a[i] = '5';
else if (a[i] == '1')
a[i] = '1';
else if (a[i] == '2')
a[i] = 'S';
else if (a[i] == '3')
a[i] = 'E';
else if (a[i] == '5')
a[i] = 'Z';
else if (a[i] == '8')
a[i] = '8';
else
{
flag = 0;
break;
}
}
for (i = len - 1; i >= 0; i--)
{
temp[x++] = a[i];
}
temp[x] = '\0';
if (flag)
if (!strcmp(temp, b))
return 1;
else
return 0;
else
return 0;
}
int mp(char *a)
{
if (pal(a) && mir(a))
return 1;
else
return 0;
}
int main()
{
char b[30];
char a[30];
char c[30];
char d[30];
char e[30];
int t;
t = 0;
while (cin >> b)
{
strcpy(a, b);
strcpy(c, b);
strcpy(d, b);
strcpy(e, b);
if (mp(a))
printf("%s -- is a mirrored palindrome.\n", a);
else
{
if (pal(c))
{
printf("%s -- is a regular palindrome.\n", c);
}
else if (mir(d))
printf("%s -- is a mirrored string.\n", d);
else
printf("%s -- is not a palindrome.\n", e);
}
printf("\n");
}
system("pause");
return 0;
}