淡泊明志 宁静致远
分类: C/C++
2006-11-23 15:38:15
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
Compares two strings for lexical order.
The comparison stops after: (1) a difference
between the strings is found,
比较字符串first和last的前n个字符。first > last
,>0;first = last,=0;first < last, <0 。
*/
int my_strncmp(const char *first,const char *last,int count)
{
if (!count)
return(0);
while
(--count && *first && *first == *last)first++,last++;
return( *(unsigned char *)first
- *(unsigned char
*)last );
}
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 3
int main()
{
char *str1=
"ammana";
char *str2 =
"babi";
Print(str1,str2,my_strncmp(str1,str2,nn),nn);
Print(str2,str1,my_strncmp(str2,str1,nn),nn);
Print(str2,str2,my_strncmp(str2,str2,nn),nn);
system("pause");
return 0;
}