Skip to content

Instantly share code, notes, and snippets.

@wmeister-old
Created March 27, 2016 01:34
Show Gist options
  • Save wmeister-old/9611565d7e20cda3bd78 to your computer and use it in GitHub Desktop.
Save wmeister-old/9611565d7e20cda3bd78 to your computer and use it in GitHub Desktop.
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