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