28 lines
557 B
C
28 lines
557 B
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
// disen 版代码: 用于比较两个字符串,如果 s1 > s2,返回 1;如果 s1 < s2,返回 -1;如果 s1 == s2,返回 0
|
||
int cmp(const char *s1, const char *s2)
|
||
{
|
||
int i = 0;
|
||
while ((s1[i] || s2[i]) && s1[i] == s2[i])
|
||
i++;
|
||
|
||
int delta = s1[i] - s2[i];
|
||
|
||
if (delta == 0)
|
||
return 0;
|
||
else if (delta > 0)
|
||
return 1;
|
||
else
|
||
return -1;
|
||
}
|
||
|
||
int main()
|
||
{
|
||
char *p = "abd";
|
||
char *q = "abcd";
|
||
printf("%d\n", cmp(p, q));
|
||
return 0;
|
||
} |