分类: C/C++
2011-04-26 23:46:47
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
Compare the two strings for lexical order. Stops the comparison when the following occurs: (1) strings differ, (2) the end of the strings is reached, or (3) count characters have been compared. For the purposes of the comparison, upper case characters are converted to lower case.
字符串比较函数,比较字符串src和dst的前count个字符,但是不区分大小写,大写字母会被转换为小写字母来进行比较。如:"abc_" < "ABCD" ,因为 "_" < "d"。当源字符串大于目标字符串的时候,返回>0;当源字符串等于目标字符串的时候,返回=0。当源字符串小于目标字符串的时候,返回<0;
*/
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;
}