This post has already been read 2061 times!


Implement a function that checks whether a positive number is a palindrome or not. For example, 121 is a palindrome, but 123 is not.


It is easy to check whether a string is a palindrome or not - we can check whether the first character and the last one are identical, and then compare the second character and the second one from the end, and so on.
If the converted string is a palindrome, the original should also be a palindrome.

int IsPalindrome(const char* const string) {

int palindrome = 1;

if(string != NULL) {
     int length = strlen(string);
     // half of string = length/2 (length >>1)
     int half = length >> 1; 
     int i;

     for(i = 0; i < half; ++ i) 
         if(string[i] != string[length - 1 - i]) 
             palindrome = 0;
         }//end if
     }//end for
}// end if

return palindrome;

Leave a Reply

Post Navigation