Last active
August 29, 2015 14:06
-
-
Save jleeothon/69d744398b805e591a0a to your computer and use it in GitHub Desktop.
C: is palindrome
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> | |
#define palsize 100 | |
/* | |
* Returns 1 if is a pure palindrome. | |
* The name is a bit mangled up because good C code shouldn't be understandable. | |
*/ | |
int ispaldrm(char* s) | |
{ | |
char *a = s, *b = s; | |
while (*b) { | |
b++; | |
} | |
b -= 1; | |
while (a < b) { | |
if (*a++ != *b--) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
int main() | |
{ | |
char s[palsize]; | |
gets(s); | |
if (ispaldrm(s)) { | |
puts("Well, yes"); | |
} else { | |
puts("Nope"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gets
is dangerous and the compiler might complain but I'm putting it for sake of simplicity againstscanf("%s", s);