本文主要是介绍字符串之strcmp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//字符串之strcmp
#include <iostream>
#include<assert.h>#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
这篇关于字符串之strcmp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!