-
-
Save wmeister-old/e0f4d737a20e7bdc3e0e 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