1
我想寫一個函數,它根據字母順序比較字符串不區分大小寫。沒有任何標準庫比較不同長度的字符串
char compare(char *x, char *y){
while (*x != '\0'){
if (tolower(*x) < tolower(*y)){
return -1;
}
else if (tolower(*x) > tolower(*y)){
return 1;
}
else{
x++;
y++;
}
}
return 0;
}
但是這個功能不能很好地與分享幾首字母(如字和雙關語)的話工作。所以我試圖修改它:
char compare(char *x, char *y){
if (len(*x) < len(*y)){
while (*x != '\0'){
if (tolower(*x) < tolower(*y)){
return -1;
}
else if (tolower(*x) > tolower(*y)){
return 1;
}
else{
x++;
y++;
}
}
// all n letters are the same (case insensitive), where n is a length of x
if (*y != '\0') return -1;
}
else {
while (*y != '\0'){
if (tolower(*x) < tolower(*y)){
return -1;
}
else if (tolower(*x) > tolower(*y)){
return 1;
}
else{
x++;
y`++;
}
}
// all n letters are the same (case insensitive), where n is a length of y
if (*x != '\0') return 1;
}
return 0;
}
但它沒有工作。我怎樣才能修改這個功能,使'文字遊戲'會比'文字'更大?
你不能用'stricmp'吧? –
存在打印錯誤,而不是++和b ++寫入x ++&y ++以及len(* x)如何工作。 – achal
@ Jean-FrançoisFabre不是我不能,我只是不想 – jakes