int strcmp(const char *s1,const char *s2);
以上为比较两个字符串,返回:
如果s1==s2,则0
如果s1>s2,则1
如果s1<s2,则-1
#include <stdio.h>
#include <string.h>
int mycmp(const char *s1,const char *s2)
{
//int idx = 0;
//while(s1[idx] == s2[idx] && s1[idx]!= '\0'){
// if(s1[idx] !=s2[idx]){
// break;
// }else if(s1[idx] == '\0'){
// break;
// }
// idx++;
//}
while (*s1==*s2&&*s1 !='\0'){
s1++;
s2++;
}
return *s1 - *s2;
}
int main(int argc,char const *argv[])
{
char s1[] = "abc";
char s2[] = "abc ";
printf("%d\n",mycmp(s1,s2));
return 0;
}