#include<iostream>
using namespace std;
int myStrCmp(char str1[], char str2[])
{
int i;
for(i = 0; str1[i] && str2[i]; i++)
{
if(str1[i] > str2[i])
return 1;
if(str1[i] < str2[i])
return -1;
}
if(!str1[i] && !str2[i]) //都已经结束
return 0;
if(!str1[i]) //str1结束,而str2还没有结束
return -1;
return 1; //str2结束,而str1还没有结束
}
int main()
{
cout << myStrCmp("computer", "computer") << endl;
cout << myStrCmp("compute", "computer") << endl;
cout << myStrCmp("computer", "compute") << endl;
cout << myStrCmp("hello", "world") << endl;
cout << myStrCmp("miss", "her") << endl;
return 0;
}
结果为:
0
-1
1
-1
1