Chinaunix首页 | 论坛 | 博客
  • 博客访问: 829296
  • 博文数量: 157
  • 博客积分: 542
  • 博客等级: 中士
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-21 20:21
文章分类
文章存档

2017年(1)

2016年(2)

2015年(6)

2014年(42)

2013年(77)

2012年(19)

2011年(10)

分类: C/C++

2013-09-25 10:14:53


点击(此处)折叠或打开

  1. char *
  2. strtok_t(char *src, const char *split)
  3. {
  4.     static char *static_src;
  5.     char *tmp_src, *tmp;
  6.     int find;

  7.     if(src != NULL)
  8.         static_src = src;

  9.     tmp_src = tmp = static_src;

  10.     if(*static_src == '\0')
  11.         return NULL;

  12.     if(split == NULL)
  13.         return tmp_src;

  14.     while(*tmp != '\0')
  15.     {
  16.         find = 0;

  17.         if(*tmp == *split)
  18.         {
  19.             find = 1;
  20.             break;
  21.         }

  22.         tmp++;
  23.     }

  24.     if(find == 1)
  25.     {
  26.         static_src = tmp + 1;
  27.         *tmp = '\0';
  28.     }
  29.     else
  30.     {
  31.         static_src = tmp;
  32.     }

  33.     return tmp_src;
  34. }





  35. int
  36. main(void)
  37. {
  38.     char test1[] = "mao,ying,chun";
  39.     char *test2 = "mao,ying,chun";

  40.     char *p;

  41.     p = strtok(test1, ",");
  42.     while(p)
  43.     {
  44.         printf("%s ", p);
  45.         p = strtok(NULL, ",");
  46.     }

  47.     printf("\n");

  48.     char test3[] = ",jonas,mao,ni,tshto,sthoista,sthoihattttt, , ,sfi";

  49.     p = strtok_t(test3, ",");
  50.     while(p)
  51.     {
  52.         printf("%s ", p);
  53.         p = strtok_t(NULL, ",");
  54.     }

  55. #if 0 // as strtok need change the const string test2, so the following code perform error.
  56.     p = strtok(test2, ",");
  57.     while(p)
  58.     {
  59.         printf("%s, ", p);
  60.         p = strtok(NULL, ",");
  61.     }
  62. #endif
  63.     printf("\n");
  64.     return 0;
  65. }

阅读(934) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~