C实现strcmp
2009-10-09 11:29 Iron 阅读(836) 评论(0) 编辑 收藏 举报比较简单就不说了,只是提醒路过的朋友,注意函数的各种情况及返回条件
#include <cstdio> int strcmpEx(const char * sa, const char * sb) { const char* stra = sa; const char* strb = sb; while(*stra!='\0'&&*strb!='\0') { if (*stra != *strb) { return *stra - *strb; } stra++;strb++; } if (*stra=='\0'&&*strb=='\0') { return 0; } else if (*stra=='\0'&&*strb!='\0') { return (int)*strb * (-1); } else { return *stra; } } int main() { printf("%d\n",strcmpEx("abcdef","abcde")); }