淡泊明志 宁静致远
分类: C/C++
2006-11-24 09:14:52
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
Compare the two strings for lexical
order. Stops the comparison when the
字符串比较函数,比较字符串src和dst的前count个字符,但是不区分大小写,大写字母
*/
int my_strnicmp(const char *dst,const char *src,int count)
{
int ch1, ch2;
do
{
if ( ((ch1
= (unsigned char)(*(dst++)))
>= 'A') &&(ch1 <= 'Z') )
ch1 += 0x20;
if ( ((ch2
= (unsigned char)(*(src++)))
>= 'A') &&(ch2 <= 'Z') )
ch2 += 0x20;
} while (
--count && ch1 && (ch1 == ch2) );
return (ch1 -
ch2);
}
void Print(char * str1,char *str2,int t,int n)
{
char *p;
p = str1;
while(*p
&& (p-str1) < n) printf("%c",*p),p++;
if(t > 0)
printf("\tUpper Than\t");
else if(t < 0)
printf("\tLower Than\t");
else
printf("\tEqual\t\t");
p = str2;
while(*p
&& (p-str2) < n) printf("%c",*p),p++;
printf("\n");
}
#define nn 4
int main()
{
char *str1=
"ammana";
char *str2 =
"babi";
char *str3 =
"AMMANA";
char *str4 =
"bab_";
Print(str1,str2,my_strnicmp(str1,str2,nn),nn);
Print(str3,str1,my_strnicmp(str3,str1,nn),nn);
Print(str4,str2,my_strnicmp(str3,str2,nn),nn);
system("pause");
return 0;
}