strcmp 库函数用于比较两个常字符串,结果为 -1、0、1.
- #include <stdio.h>
-
-
int strcmp_test(const char *cs, const char *ct);
-
-
int
-
main(int argc, char **argv)
-
{
-
char *s1 = "abc123";
-
char *s2 = "abC123";
-
-
printf("Result: %s - %s = %d\n", s1, s2, strcmp_test(s1, s2));
-
-
return 0;
-
}
-
-
int
-
strcmp_test(const char *cs, const char *ct)
-
{
-
unsigned char c1, c2;
-
-
while (1) {
-
c1 = *cs++;
-
c2 = *ct++;
-
if (c1 != c2) {
-
return c1 < c2 ? -1 : 1;
-
}
-
if (!c1) {
-
break;
-
}
-
}
-
-
return 0;
-
}
注:三目运算符的运用
阅读(316) | 评论(0) | 转发(0) |