//字符串之strcmp
#include <iostream>
#include<string.h>
using namespace std;
int strcmpT(const char * a,const char * b)
{
assert(a!=NULL&&b!=NULL);
int ret=0;
while(!(ret=*(unsigned char *)a-*(unsigned char *)b)&&(*a))
{
a++;
b++;
}
if(ret<0)
ret=-1;
else if(ret>0)
ret=1;
return ret;
}
int main()
{
cout << strcmpT("Hello world!","hello baby") << endl;
cout << strcmp("Hello world!","hello baby") << endl;
cout << strcmpT("","hello baby") << endl;
cout << strcmp("","hello baby") << endl;
cout << strcmpT("hello baby","hello baby") << endl;
cout << strcmp("hello baby","hello baby") << endl;
cout << strcmpT("","") << endl;
cout << strcmp("","") << endl;
cout << strcmpT("abcd","ab") << endl;
cout << strcmp("abcd","ab") << endl;
return 0;
}
输出结果:
-1
-1
-1
-1
0
0
0
0
1
1