写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于str2返回1,若str1小于str2返回-1,不调用C++/C的字符串的字符库函数,请编写函数strcmp,函数定义为:intstrcmp(const char*src,const char*dst)
#include<stdio.h>
int strcmp(const char *src,const char *dst)
{
int ret = 0;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst)&&*src&&*dst)
src++,dst++;
if(ret < 0)
ret = -1;
else
if(ret > 0)
ret = 1;
return ret;
}
int main(int argc,char *argv[])
{
char *str1 = "ab";
char *str2 = "abd";
printf("%d\n",strcmp(str1,str2));
return 0;
}
![](https://img-my.csdn.net/uploads/201212/11/1355203096_6680.jpg)