2009年7月21日 星期二

自訂一個strcmp (c++)



#include <iostream>
using namespace std;
int strcmp2(const char *dest, const char *ori);
int main(void){
cout<<strcmp2("aello","a")<<endl;
return 0;
}
int strcmp2(const char *dest, const char *ori){
assert(ori !=NULL && dest!=NULL);
int tmp =0;
while(!(tmp = *(unsigned char*)dest - *(unsigned char*)ori)&&*ori){
++dest;
++ori;
}
if(tmp <0){
tmp= -1;
}else if(tmp >0){
tmp=1;
}
return tmp;
}

沒有留言:

張貼留言