Determine whether an integer is a palindrome. Do this without extra space.
unsigned short num_digits(int num)
{
long long int div = 1;
int count = 0;
while (div <= num) {
count++;
div *= 10;
}
return count;
}
unsigned short digitat(int num, int at, int num_digits)
{
return int(num / pow(10, num_digits - at - 1)) % 10;
}
bool isPalindrome(int x)
{
if (x < 0) return false;
const int numdig = num_digits(x);
int start = 0;
int end = numdig - 1;
while (start < end) {
int ok = digitat(x, start, numdig);
int ok2 = digitat(x, end, numdig);
if (digitat(x, start, numdig) != digitat(x, end, numdig))
return false;
start++; end--;
}
return true;
}