/* Details: Palindrome code using pointer
* Author : Vinod Sasidharan
* Date : Wednesday, March 16, 2011, 10.14 am
* Remarks: Multiple words possible in input string due
* to use of fgets.
*/
/* Include files */
#include <stdio.h>
#include <string.h>
/* Function prototype declaration */
unsigned char palindrome(char *s);
/* Function name: Palindrome
* Input : Pointer to a string
* Output : 8bit unsigned - 0/1
*/
unsigned char palindrome(char *s)
{
char *s2 = s + strlen(s) - 1;
/* Sanity check */
if(!*s) return 0;
while(*(s2--) == *(s++) && *s);
return((unsigned char)(!*s && *(++s2) == *(--s)));
}
/* Main function */
int main()
{
char buff[35];
fputs("Input string: ",stdout);
fgets(buff,sizeof(buff),stdin);
fflush(stdin);
/* To avoid the \n in the input string */
*(buff + strlen(buff) - 1) = '\0';
printf("Is the string %s palindrome: %s\n",buff,
palindrome(buff)?"YES":"NO");
getchar();
return 0;
}
Tested on: Visual C++ 2008 Express Edition, Windows XP
No comments:
Post a Comment