String palindrome

Enter a string, filter out non-alphabetic characters, and then determine whether it is a palindrome, regardless of case.
Input description
Input a string, filter out non-alphabetic characters in it
Output
description Output "Y" or "N"
Input example
Madam, I'm a dam!
Hello, world~~
Output example
Y
N
Note: Palindrome Refers to a centrosymmetric string. This question is very simple, mainly to remember the algorithm of filtering characters and how to compare inward step by step from end to end.

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    int i, j, flag=1;
    char a[100];
    gets(a);
    for (j=i=0; a[i]; i++){//过滤非字母字符
        if (a[i]>='A'&&a[i]<='Z' || a[i]>='a'&&a[i]<='z')
            a[j++] = a[i];//j是在赋值完后才自增1
    }
    a[j] = '\0';//令a[j]='\0'作为字符串结束标志
    for (i=0, j--; i<j; i++, j--){//j必须先减一,因为a[j]='\0'
        if (a[i]!=a[j] && fabs(a[i]-a[j])!=32){//相等的判定是同字母或者大小写
            flag = 0;
            break;
        }
    }
    if (flag==1) printf("Y");
    else printf("N");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325584410&siteId=291194637