Reputation: 13
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
?
Upvotes: 1
Views: 2558
Reputation: 726579
Sure - you can compare the results of tolower
right in the if
statement:
while (*x1 && *x2 && tolower(*x1) == tolower(*x2)) {
x1++;
x2++;
}
return tolower(*x1)-tolower(*x2);
Upvotes: 2
Reputation: 5006
You could use a temporary char variable:
char c1 = tolower(*x1);
char c2 = tolower(*x2);
if (c1 == c2)
...
Upvotes: 2