From 348f5d76e5fefa98a80b91ae3ef9bdd90a6e4f1e Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Mon, 17 Jan 2022 07:51:48 -0600 Subject: [PATCH] c/palindrome.c: check if a word is a valid palindrome --- c/palindrome.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 c/palindrome.c diff --git a/c/palindrome.c b/c/palindrome.c new file mode 100644 index 0000000..36b55f7 --- /dev/null +++ b/c/palindrome.c @@ -0,0 +1,21 @@ +int palindrome(const char *str, unsigned len) { + unsigned idx = 0; + + for(; idx < (len - (len % 2)) / 2; idx += 1) { + if(str[idx] != str[len - idx - 1]) { + return 0; + } + } + + return 1; +} + +#include + +int main(int argc, char **argv) { + if(argc < 2) { + return 2; + } + + return !palindrome(argv[1], strlen(argv[1])); +}