UVa401 Palindromes
#include <cstdio>
#include <cctype>
#include <cstring>
const char* reverse
= "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg
[] = {"not a palindrome.",
"a regular palindrome.",
"a mirrored palindrome.",
"a mirrored string."};
bool isMirrored(const char c1
, const char c2
)
{
if (isdigit(c1
))
{
return reverse
[c1
-'0'+25] == c2
;
}
else
{
return reverse
[c1
-'A'] == c2
;
}
}
int type(const char* str
)
{
bool isPal
= true;
bool isMir
= true;
int len
= strlen(str
);
for (size_t i
= 0; i
<= len
/2; i
++)
{
if (str
[i
] != str
[len
-i
-1]) isPal
= false;
if (!isMirrored(str
[i
], str
[len
-i
-1])) isMir
= false;
if (!(isPal
|| isMir
)) break;
}
if (isPal
+ isMir
== 1)
{
if (isPal
)
{
return 1;
}else
{
return 3;
}
}
else
{
return isPal
+ isMir
;
}
}
int main()
{
char str
[25];
while ( scanf("%s", str
) != EOF)
{
printf("%s -- is %s\n\n", str
, msg
[type(str
)]);
}
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-2163.html