简体   繁体   English

如何编写适当的strcmp?

[英]How do I write a proper strcmp?

Below is the code I've written for strcmp , I want it to not differentiate between uppercase and lowercase letters, However it still does, How do I fix this? 以下是我为strcmp编写的代码,我希望它不区分大写和小写字母,但是仍然如此,如何解决此问题?

int strcmp(char str1[], char str2[]) {
    int i = 0;
    for (; str1[i] || str2[i];) {
        if (str1[i] > str2[i]) {
            int j = (str1[i] - str2[i]);
            if (j == 32)
                i++;
            else {
                return +1;
            }
        } else
        if (str1[i] < str2[i]) {
            int q = (str1[i] - str2[i]);
            if (q == -32) {
                i++;
            } else {
                return -1;
            }
        } else
        if (str1[i] == str2[i]) {
            i++;
        }
    }
    return 0;
}

Example: Input: 示例:输入:

Aryan
Semi
Kim
kim
Nap

Output: 输出:

Aryan
Kim
Nap
Semi
kim

For example: 例如:

int strcasecmp(const char *a, const char *b)
{
    size_t i;
    for (i = 0; tolower((unsigned char)a[i]) == tolower((unsigned char)b[i]) && a[i]; ++i);
    return tolower((unsigned char)a[i]) - tolower((unsigned char)b[i]);
}

There are multiple problems with your function: 您的函数存在多个问题:

  • do not name is strcmp() . 不要命名为strcmp() You should not redefine standard functions with different semantics. 您不应使用不同的语义重新定义标准函数。 strcmp() is usually highly optimized, it is possible that your version is not even the one used when you pass strcmp to your sorting function. 通常对strcmp()进行高度优化,将strcmp传递给排序函数时,您的版本可能甚至不可用。

  • The algorithm is incorrect: any 2 characters 32 positions apart are considered equal, for example "0" == "P". 该算法不正确:相隔32个位置的任意2个字符都视为相等,例如“ 0” ==“ P”。

  • The comparison is non transitive: you have "A" < "_" and "_" < "a" yet "A" == "a" , which is very problematic for sorting. 比较是非传递性的:您具有"A" < "_""_" < "a""A" == "a" ,这对于排序非常有问题。

  • You should not assume ASCII and hard code the case offsets. 您不应该假设ASCII和硬编码区分大小写。 Use toupper() from <ctype.h> and cast char values as (unsigned char) to avoid undefined behavior on negative values. 使用<ctype.h> toupper()并将char<ctype.h>(unsigned char)以避免对负值进行未定义的行为。

  • i should be a size_t . i应该是size_t

  • str1 and str2 should be const qualified. str1str2应该是const限定的。

Here is an improved version: 这是一个改进的版本:

#include <ctype.h>

int strcmp_case(const char *str1, const char *str2) {
    for (size_t i = 0;; i++) {
        int c1 = toupper((unsigned char)str1[i]);
        int c2 = toupper((unsigned char)str2[i]);
        if (c1 != c2) {
            return (c1 > c2) - (c1 < c2);
        }
        if (c1 == '\0') {
            return 0;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM