Case Insensitive String comp in C

前端 未结 11 1199
天涯浪人
天涯浪人 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条回答
  •  猫巷女王i
    2020-11-27 04:16

    int strcmpInsensitive(char* a, char* b)
    {
        return strcmp(lowerCaseWord(a), lowerCaseWord(b));
    }
    
    char* lowerCaseWord(char* a)
    {
        char *b=new char[strlen(a)];
        for (int i = 0; i < strlen(a); i++)
        {
            b[i] = tolower(a[i]);   
        }
        return b;
    }
    

    good luck

    Edit-lowerCaseWord function get a char* variable with, and return the lower case value of this char*. For example "AbCdE" for value of char*, will return "abcde".

    Basically what it does is to take the two char* variables, after being transferred to lower case, and make use the strcmp function on them.

    For example- if we call the strcmpInsensitive function for values of "AbCdE", and "ABCDE", it will first return both values in lower case ("abcde"), and then do strcmp function on them.

提交回复
热议问题