Case Insensitive String comp in C

前端 未结 11 1192
天涯浪人
天涯浪人 2020-11-27 03:45

I have two postcodes char* that I want to compare, ignoring case. Is there a function to do this?

Or do I have to loop through each use the tolower func

11条回答
  •  醉话见心
    2020-11-27 04:20

    There is no function that does this in the C standard. Unix systems that comply with POSIX are required to have strcasecmp in the header strings.h; Microsoft systems have stricmp. To be on the portable side, write your own:

    int strcicmp(char const *a, char const *b)
    {
        for (;; a++, b++) {
            int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
            if (d != 0 || !*a)
                return d;
        }
    }
    

    But note that none of these solutions will work with UTF-8 strings, only ASCII ones.

提交回复
热议问题