This problem requires writing a function to determine whether a given string of characters is a "palindromic". The so-called "palindrome" refers to the same string of forward reading and backward reading. Such as "XYZYX" and "xyzzyx" are palindrome.
Function interface definition:
bool palindrome( char *s );
The function palindrome
determines whether the input string char *s
is a palindrome. If so, return true
, otherwise return false
.
Example of the referee test procedure:
#include <stdio.h> #include <string.h> #define MAXN 20 typedef enum {false, true} bool; bool palindrome( char *s ); intmain () { char s[MAXN]; scanf("%s", s); if ( palindrome(s)==true ) printf("Yes\n"); else printf("No\n"); printf("%s\n", s); return 0; } /* Your code will be embedded here */
Input sample 1:
thisistrueurtsisiht
Sample output 1:
Yes
thisistrueurtsisiht
Input sample 2:
thisisnottrue
Sample output 2:
No
thisisnottrue
Author: C course group
Unit: Zhejiang University
Time limit: 400ms
Memory Limit: 64MB
Code length limit: 16KB
my code:
bool palindrome(char *s) { bool equal = true; int i, j; int len = strlen(s) - 1; for (i = 0, j = len; j - i > 0; i++, j--) { if (s[i] - s[j]) { equal = false; break; } } return equal; }