Case Insensitive String comp in C

前端 未结 11 1122
天涯浪人
天涯浪人 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:02

    You can get an idea, how to implement an efficient one, if you don't have any in the library, from here

    It use a table for all 256 chars.

    • in that table for all chars, except letters - used its ascii codes.
    • for upper case letter codes - the table list codes of lower cased symbols.

    then we just need to traverse a strings and compare our table cells for a given chars:

    const char *cm = charmap,
            *us1 = (const char *)s1,
            *us2 = (const char *)s2;
    while (cm[*us1] == cm[*us2++])
        if (*us1++ == '\0')
            return (0);
    return (cm[*us1] - cm[*--us2]);
    

提交回复
热议问题