Created
January 30, 2012 06:00
-
-
Save Jookia/1702831 to your computer and use it in GitHub Desktop.
palindrome.c
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
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It reminds me of some old Programming Praxis at thedailywtf http://thedailywtf.com/Series/Bring_Your_Own_Code.aspx