Skip to content

Instantly share code, notes, and snippets.

@Jookia
Created January 30, 2012 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jookia/1702831 to your computer and use it in GitHub Desktop.
Save Jookia/1702831 to your computer and use it in GitHub Desktop.
palindrome.c
#include <stdio.h>
#include <string.h>
int is_palindrome(const char* text)
{
/* Set the forward and backward iterators to outside
the string as they move in the first iteration. */
const char* forward = &text[0] - 1;
const char* backward = &text[strlen(text)];
/* Loop until the iterator has passed the middle,
or until the characters don't match. */
while(!(forward > backward || *(++forward) != *(--backward)));
/* If the middle has been passed, that means that all of the
characters up to (and including) the middle match.
Thus it's a palindrome. */
return (forward >= backward);
}
int main(void)
{
printf("racecar: %i\nnoimnot: %i\n",
is_palindrome("racecar"),
is_palindrome("noimnot"));
return 0;
}
@madrang
Copy link

madrang commented Jan 30, 2012

It reminds me of some old Programming Praxis at thedailywtf http://thedailywtf.com/Series/Bring_Your_Own_Code.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment