我試圖用這個函數比較兩個字符串,不區分大小寫。常量字符串不區分大小寫比較
int strcasecmp(const char *x1, const char *x2);
我有複製件正確的,可區分大小寫的部分是給我帶來些麻煩爲const是一個常量,因此只讀取,使得這些失敗:
*x1 = (tolower(*x1)); // toupper would suffice as well, I just chose tolower
*x2 = (tolower(*x2)); // likewise here
兩個字符必須保持const
,否則我認爲這將工作... 所以我的問題:有沒有辦法忽略大寫,同時保持char
-strings const
?
那麼你爲什麼不只是使用一個臨時要存儲的值(tolower(* x))? – Marcus 2012-07-10 02:09:59
將數據複製到非常量字符數組中,將所有字符轉換爲小寫字母。 – 2012-07-10 02:10:00
@Marcus我不允許將* x1或* x2更改爲除const char – user1513475 2012-07-10 02:13:21