Case Insensitive String comp in C

前端 未结 11 1166
天涯浪人
天涯浪人 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 03:55

    Simple solution:

    int str_case_ins_cmp(const char* a, const char* b) {
      int rc;
    
      while (1) {
        rc = tolower((unsigned char)*a) - tolower((unsigned char)*b);
        if (rc || !*a) {
          break;
        }
    
        ++a;
        ++b;
      }
    
      return rc;
    }
    

提交回复
热议问题