我自己谢谢的,看看吧#include<iostream.h>#include<assert.h>#include<string.h>int strcmp(char s1[ ],char s2[ ]){ assert(strlen(s1)!=0&&strlen(s2)!=0); char *p1,*p2; p1=s1;p2=s2; while(*p1!=NULL&&*p2!=NULL) { if(*p1>*p2) return 1; else if(*p1<*p2) return -1; else { p1++; p2++; } } if(*p1==NULL&&*p2!=NULL) //串2长,因此串2大 return -1; else if(*p1!=NULL&&*p2==NULL) //串1长,因此串1大 return 1; else if(*p1==NULL&&*p2==NULL) //都为NULL ,相等 return 0; }int main(){ char s1[]="hello"; char s2[]="hello world!"; int i=strcmp(s1,s2); switch(i) { case 0 : cout<<"s1==s2"<<endl; break; case 1 : cout<<"s1>s2"<<endl; break; case -1: cout<<"s1<s2"<<endl; break; }return 0;}
int cmp(char *p,char *q){ while(*p!='\0' && *q!='\0' && *p==*q) { p++; q++; } if(*p>*q) return 1; if(*p<*q) return -1; return 0;}
int mystrcmp(const char* str1, const char* str2){ assert((str1 != NULL) && (str2 != NULL)); int ret = 0; while (!(ret = *(unsigned char*)str1 - *(unsigned char*)str2) && *str2) { str1++; str2++; } if (ret > 0) ret = 1; else if (ret < 0) ret = -1; return ret;}