z_archive/problem36/ProjectEuler36/main.cpp
// Checks if a number is a palindrome.
// Can also check the base-two representation of that number.
Checks if a number is a palindrome.
Can also check the base-two representation of that number.
Checks if a number is a palindrome . Can also check the base - two representation of that number .
static bool isPalindrome(int number, bool base2)
{
    char numAscii[33] = {};
    char * numAsciiStart = numAscii;
    if (base2)
    {
        int2bin(number, numAscii, sizeof(numAscii) - 1);
        while (*numAsciiStart && *numAsciiStart != '1')
        {
            numAsciiStart++;
        }
    }
    else
    {
        snprintf(numAscii, sizeof(numAscii), "%d", number);
    }
    char * ptr1 = numAsciiStart;
    char * ptr2 = numAsciiStart;
    char * tempPtr = numAsciiStart;
    while (*tempPtr)
    {
        tempPtr++;
        if (!*tempPtr)
        {
            break;
        }
        ptr2++;
    }
    bool isPalindrome = true;
    while (ptr1 < ptr2)
    {
        if (*ptr1 != *ptr2)
        {
            isPalindrome = false;
            break;
        }
        ptr1++;
        ptr2--;
    }
    return isPalindrome;
}
static bool isPalindrome ( int number , bool base2 ) { char numAscii [ 33 ] = { } ; char * numAsciiStart = numAscii ; if ( base2 ) { int2bin ( number , numAscii , sizeof ( numAscii ) - 1 ) ; while ( * numAsciiStart && * numAsciiStart != ' ' ) { numAsciiStart ++ ; } } else { snprintf ( numAscii , sizeof ( numAscii ) , " " , number ) ; } char * ptr1 = numAsciiStart ; char * ptr2 = numAsciiStart ; char * tempPtr = numAsciiStart ; while ( * tempPtr ) { tempPtr ++ ; if ( ! * tempPtr ) { break ; } ptr2 ++ ; } bool isPalindrome = true ; while ( ptr1 < ptr2 ) { if ( * ptr1 != * ptr2 ) { isPalindrome = false ; break ; } ptr1 ++ ; ptr2 -- ; } return isPalindrome ; }
Checks if a number is a palindrome.
Checks if a number is a palindrome .
// Put numAsciiStart at the first '1' //cout << numAsciiStart << endl; // Get the last char of this string // At this point, ptr1 is the start and ptr2 is the end. // So start checking if it's a palindrome.