2010-09-14 #include<stdio.h> #include<string.h> #include<ctype.h> int strnicmp(const char *s1, const char *s2, int len) { unsigned char c1, c2; if(!len) return 0; do{ c1 = *s1++; c2 = *s2++; if (!c1 || !c2) break; if (c1 == c2) continue; c1 = tolower(c1); c2 = tolower(c2); if (c1 != c2) break; }while(--len); return (int)c1 - (int)c2; } int main(void) { const char str1[] = "ABC"; const char str2[] = "AbCD"; printf("strnicmp(str1, str2, strlen(str1)) = %d/n", strnicmp(str1, str2, strlen(str1))); return 0; } 函数名: strnicmp 功 能: 比较字符串str1和str2的前n个字符串字典序的大小,但是不区分字母大小写。 运行结果: [root@localhost myfunc]# ./my_strnicmp strnicmp(str1, str2, strlen(str1)) = 0