/************************************
Write a program that reads in a string
from the user and tells the user whether
or not it is a palindrome.
************************************/
#include <stdio.h>
#include <string.h>
int reverses(char* s1, char* s2);
int main() {
// Get string from used
char word[128];
printf("Enter string: ");
fflush(stdout);
scanf(" %s", word);
// Print whether A and B are reverses
if (reverses(word, word)) {
printf("It's a palindrome!\n");
} else {
printf("Not a palindrome.\n");
}
return 0;
}
// Tests whether s1 and s2 are reverses of one another
int reverses(char* s1, char* s2) {
// Get lengths and make sure they're equal!
int len = strlen(s1);
if (len != strlen(s2)) {
return 0;
}
// test forward on s1 and reverse on s2
int fwd = 0;
int rev = len - 1;
while (rev >= 0) {
if (s1[fwd] != s2[rev]) {
return 0;
}
++fwd;
--rev;
}
return 1;
}