Created
March 27, 2016 01:34
-
-
Save wmeister-old/9611565d7e20cda3bd78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool is_palindrome(ullong n) | |
{ | |
char s[ULLONG_MAX_DIGITS+1]; | |
sprintf(s, "%i", n); | |
if(strlen(s) == 1) | |
{ | |
return true; | |
} | |
else if(strlen(s) == 2) | |
{ | |
if(s[0] == s[1]) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
else if(strlen(s) >= 3) | |
{ | |
while(strlen(s) > 2) | |
{ | |
if(s[0] != s[strlen(s) - 1]) | |
{ | |
return false; | |
} | |
char *t = (char*) malloc(sizeof(char)*(ULLONG_MAX_DIGITS + 1)); | |
strncpy(t, &s[1], strlen(s) - 2); | |
t[strlen(s) - 2] = '\0'; | |
strcpy(s, t); | |
} | |
if(strlen(s) == 2 && s[0] != s[1]) | |
{ | |
return false; | |
} | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment