so true

心怀未来,开创未来!
随笔 - 160, 文章 - 0, 评论 - 40, 引用 - 0
数据加载中……

我给出的strcmp源码

源码:

int strcmp(const char * str1, const char *str2){
 do{
  if(!str1 || !*str1)return (!str2 || !*str2) ? 0 : -1;
  if(!str2 || !*str2)return 1;
 }while(*str1++==*str2++);
 return *(str1-1)-*(str2-1);
}

测试:

 cout<<strcmp(NULL,NULL)<<endl;
 cout<<strcmp("abc",NULL)<<endl;
 cout<<strcmp(NULL,"abc")<<endl;
 
 cout<<strcmp("abc","abc")<<endl;
 cout<<strcmp("abcd","abc")<<endl;
 cout<<strcmp("abc","abcd")<<endl;
 cout<<strcmp("abc","de")<<endl;
 cout<<strcmp("de","abc")<<endl;
 cout<<strcmp(" ","\n")<<endl;

结果:

0
1
-1
0
1
-1
-3
3
22
Press any key to continue

posted on 2008-09-04 13:31 so true 阅读(602) 评论(0)  编辑  收藏 所属分类: C&C++


只有注册用户登录后才能发表评论。


网站导航: