Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87312
  • 博文数量: 60
  • 博客积分: 4002
  • 博客等级: 中校
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 18:11
文章分类

全部博文(60)

文章存档

2011年(60)

我的朋友

分类: C/C++

2011-01-01 18:31:12

    strcmp 库函数用于比较两个常字符串,结果为 -1、0、1.

  1. #include <stdio.h>

  2. int strcmp_test(const char *cs, const char *ct);

  3. int
  4. main(int argc, char **argv)
  5. {
  6.     char *s1 = "abc123";
  7.     char *s2 = "abC123";

  8.     printf("Result: %s - %s = %d\n", s1, s2, strcmp_test(s1, s2));

  9.     return 0;
  10. }

  11. int
  12. strcmp_test(const char *cs, const char *ct)
  13. {
  14.     unsigned char c1, c2;

  15.     while (1) {
  16.         c1 = *cs++;
  17.         c2 = *ct++;
  18.         if (c1 != c2) {
  19.             return c1 < c2 ? -1 : 1;
  20.         }
  21.         if (!c1) {
  22.             break;
  23.         }
  24.     }

  25.     return 0;
  26. }
注:三目运算符的运用
阅读(307) | 评论(0) | 转发(0) |
0

上一篇:strcpy

下一篇:strcat

给主人留下些什么吧!~~