Monday, September 19, 2011

#C_CODE: Palindrome

#include <stdio.h>
#include <string.h>


int pali(char *s)
{
    char *s2 = s + strlen(s) - 1;

    if(!*s) return 0;

    while(*(s2--) == *(s++) && *s);
    return (!*s && *(++s2) == *(--s));
}

int main()
{  
    char buff[32];
    fputs("Enter the string: ",stdout);
    fgets(buff,sizeof(buff),stdin);  
    *(buff + strlen(buff) - 1)  = '\0';
    printf("Is the string \"%s\" palindrome?%s\n",  

    buff,pali(buff)?"YES":"NO");
    return 0;
}

No comments: