如何实现strcmp?
如何编程实现int strcmp(char *s1,char *s2)?
如何编程实现int strcmp(char *s1,char *s2)?
#include <stdio.h>
int mystrcmp(char *s1, char *s2)
{
while((*s1 == *s2) && *s1 != '\0')
{
s1++;
s2++;
}
return *s1 - *s2;
}
int main()
{
char *s1 = "hello";
char *s2 = "hello";
char *s3 = "help";
printf("%d\n", mystrcmp(s1, s2));
printf("%d\n", mystrcmp(s2, s3));
return 0;
}