淡泊明志 宁静致远
分类: C/C++
2006-11-25 15:15:48
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
memicmp perform a case-insensitive memory
comparision.
For differences,upper case letters are mapped to lower
case.Thus, "abc_" < "ABCD" since "_" <
"d".
(与memcmp区别就是在比较的时候不区分大小写)比较内存区域buffer1和buffer2的前count个字节。
*/
int my_tolower(char ch)
{
if(ch >=
'A' && ch <= 'Z')
return (ch
+ 0x20);
return ch;
}
int my_memicmp(const void *buffer1,const void *buffer2,int
count)
{
int f = 0;
int l = 0;
while
(count--)
{
if (
(*(unsigned char
*)buffer1 == *(unsigned char *)buffer2) ||
((f = my_tolower( *(unsigned char
*)buffer1 )) ==
(l = my_tolower( *(unsigned
char *)buffer2 ))) )
{
buffer1 = (char
*)buffer1 + 1;
buffer2 = (char
*)buffer2 + 1;
}
else
break;
}
return ( f
- l );
}
void Print(char * str1,char *str2,int t)
{
if(t > 0)
printf("\n%s Upper Than
%s\n",str1,str2);
else if(t < 0)
printf("\n%s Lower Than
%s\n",str1,str2);
else
printf("\n%s Equal %s\n",str1,str2);
}
int main()
{
char *str1=
"ammana";
char *str2 =
"babi";
char *str3 =
"AMMANA";
char *str4 =
"bab_";
Print(str1,str2,my_memicmp(str1,str2,4));
Print(str3,str1,my_memicmp(str3,str1,4));
Print(str4,str2,my_memicmp(str4,str2,4));
system("pause");
return 0;
}