Hmmm this has some limitations... but what if you did this:
Example:
Code:
static const int primes[] = {02,03,05,07,013,015,021,023,027}; // yep.. I used octals... I am bored
long unsigned long key_fstring(const char *s)
{
long unsigned long r = 0;
if(s)
for(r = 1;*s;++s)
r *= primes[*s - '0'];
return r;
}
long unsigned long key_rstring(const char *s)
{
long unsigned long r = 0;
const char *begin = s;
if(s)
for(r = 1, s += strlen(s) - 1;s >= begin;--s)
r *= primes[*s - '0'];
return r;
}
int main(void)
{
char buffer[256], *p, *end;
int x;
do
{
puts("Please input a number:");
fgets(buffer, sizeof(buffer), stdin);
strtok(buffer, "\n");
x = strtol(buffer, &end, 10);
if(end != buffer)
{
x = +x;
snprintf(buffer, sizeof(buffer), "%d", x);
/* All that has been done up to this point is the gathering of the user input, less user idiocy */
printf("%d is%sa palindrome.\n", x, (key_fstring(buffer) == key_rstring(buffer))?" "," not ");
}
} while(*buffer);
return 0;
}