如何忽略符号& C

时间:2018-03-05 01:27:02

标签: c

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

int main(int argc, char **argv) {

    int l = 0;
    char a[] = "Madam, I'm Adam.";
    int h = strlen(a) - 1;
    while (h > 1) {
        if (a[l++] != a[h--]) {
            printf("%s is not a palindrome\n", a);
            return 1;
        }    

    }

}

这适用于像“女士”这样没有任何符号的字符串。有没有办法忽略所有符号,如“。”,“”,“'”,实际上,所有非字母数字字符。有没有办法使这项工作?

2 个答案:

答案 0 :(得分:1)

您可以使用 from PIL import Image, ExifTags import glob metadata =[] def extract_metadata(filename): # extract metadata image = Image.open(filename) exif = { ExifTags.TAGS[k]: v for k, v in image._getexif().items() if k in ExifTags.TAGS } model = exif['Model'] datetime =exif['DateTimeDigitized']#creates a list from the fields required return model + '_' + datetime # Loop through all jpeg files and create a list for metadatda extraction for each for image_file in glob.glob("path/*.jpg"): md_file = extract_metadata(image_file) metadata.append((f"{image_file} {md_file}"))#convert output into a list metadata 功能测试字母数字字符。如果您遇到一个不是的,请根据需要递增/递减索引,直到找到一个。

isalnum

答案 1 :(得分:0)

isalnum检查将检查字符是否为字母数字。在比较之前,tolower将小写字符。我为角落案例添加了这些检查和一些逻辑。看看这是否有效。它适用于我的Mac。

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

int main(int argc, char **argv) {

    int l = 0;
    char a[] = "Madam, I'm Adam.";
    int h = strlen(a) - 1;
    int match = 0;
    while (h > 1 && l <= h) {
        if (!isalnum (a[h])) {
            h--;
            continue;
        }
        if (!isalnum (a[l])) {
            l++;
            continue;
        }
        if (tolower(a[l++]) != tolower(a[h--])) {
            printf("%s is not a palindrome\n", a);
            return 1;
        }
        match = 1; /* at least one alphanum haracter match */
    }

    if (match == 1) {
        /* We need at least one true alphanum character match */
        printf("%s is a palindrome\n", a);
        return 0;
    }
    else
    {
        printf ("String of special chars %s \n", a);
        return 1;
    }
}