qfedu-c-level/day11/d13_2.c

28 lines
557 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}