Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209631
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-21 20:26:02

  1. /* k&r(5.5): character pointers and functions
  2.    created on Mar 21, 2011
  3.    */
  4. #include "stdio.h"

  5. int pstrcmp(char *, char *);
/* main: compares two character strings s and t, and return negative, zero or position if s t!
The value is obtained by substracting the characters at the first position where s and t disagree.
*/
  1. int main()
  2. {
  3.     int i;
  4.     char str1[] = "hello";
  5.     char str2[] = "hello mark";

  6.     i = pstrcmp(str1, str2);
  7.     printf("%d\n", i);
  8. }

  9. /* strcmp: return <0 if s0 if s>t */
  10. int astrcmp(char *s, char *t)
  11. {
  12.     int i;

  13.     for (i = 0; s[i] == t[i]; i++)
  14.      if (s[i] == '\0')
  15.         return 0;
  16.     return s[i] - t[i];
  17. }

  18. /* strcmp: pointer version */
  19. int pstrcmp(char *s, char *t)
  20. {
  21.     for(; *s == *t; s++, t++)
  22.      if (*s == '\0')//also *t=='\0'
  23.         return 0;
  24.     return *s - *t;
  25. }
阅读(374) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~