1

I'm trying to use this function to compare two strings, case insensitively.

int strcasecmp(const char *x1, const char *x2);

I have the copy piece correct, yet the case sensitive portion is giving me some trouble as const is a constant, thus read only, making these fail:

*x1 = (tolower(*x1)); // toupper would suffice as well, I just chose tolower
*x2 = (tolower(*x2)); // likewise here

Both chars must remain const, otherwise I think this would work... So my question: is there a way to ignore capitalization while keeping the char-strings const?

4

2 回答 2

2

您可以使用临时 char 变量:

char c1 = tolower(*x1);
char c2 = tolower(*x2);

if (c1 == c2)
 ...
于 2012-07-10T02:12:10.473 回答
2

当然 - 您可以在语句中比较tolowerright的结果:if

while (*x1 && *x2 && tolower(*x1) == tolower(*x2)) {
    x1++;
    x2++;
}
return tolower(*x1)-tolower(*x2);
于 2012-07-10T02:16:00.447 回答