int palindrome_check(char *s, int len, int x) { if(x >= len) { return 1; } if(s[x] != s[len - x - 1]) { return 0; } return palindrome_check(s, len, x + 1); }
int gen(int a, int b) { int largest_palindrome = 0; while (a > 0) { int multiplier = b; while (multiplier > 0) { int product = a * multiplier; if (palindrome_check(product)) { if (product > largest_palindrome) { largest_palindrome = product; } } multiplier -= 1; } a -= 1; } return largest_palindrome; }
/**************************************************************************** * * Function Name : main * Description : Demonstration basic string concepts * Returns : Success or Failure * ****************************************************************************/ int main() { char input[NAMESZ]; /* Variable to get input from user */ char reverse[NAMESZ]; /* Variable to store reverse of string */ char *fgets_ret = NULL; /* Return Value from fgets */ /* Initializing the local variables */ memset(input, 0, NAMESZ); memset(reverse, 0, NAMESZ); printf("Enter a string (Size < 32)\n"); /* Observe the waring given by compiler for the following line. Compiler gives the warning since there is no array bound check for gets */ /* Same issue is there with scanf also */ // gets(input); /* fgets has an option to specify the array bound */ fgets_ret = fgets(input, NAMESZ,stdin); if(NULL != fgets_ret) { /* fgets reads a maximum of NAMESZ-1 characters. Last position is reserved for '\0'. But there could be a '\n' before '\0' if the number of characters were less than NAMESZ-1. So we need to remove it */ remove_newline(input); /* reversing the string */ str_rev(input, reverse); printf("Original string is %s\n", input); printf("Original string is %s\n", reverse); /* Checking for palindrome */ palindrome_check(input,reverse); } else { printf("fgets failed\n"); } return 0; }
int is_palindrome(char *s) { int str_len; str_len = length(s, 0); return palindrome_check(s, str_len ,0); }