免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5457 | 回复: 6
打印 上一主题 下一主题

[C] strtok分割字符串的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-31 15:43 |只看该作者 |倒序浏览
输入:11|22|33|44

  1. int main( int argv, char * argc )
  2. {
  3.      if( argv < 2) return 0;

  4.      char *delim = "|";
  5.      char *p;
  6.   
  7.      p = strtok( argc[1], delim )
  8.      printf( "%s \n", p);

  9.      while( p = strtok( NULL, delim ) )
  10.      {
  11.           printf( "%s \n", p);
  12.       }

  13.       return 0;
  14. }
复制代码
这段代码没有什么问题,
但是如果输入里有一项是空的时候:例如  11|22||44
这时候就只能得到 11   22  44
而我想要的是还是得到4个结果  只是第三个是空的就可以了

不知道这个可以这么实现呢?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2013-07-31 16:03 |只看该作者
  1. #include <stdio.h>
  2. #include <string.h>

  3. void split001( const char* str, const char chr )
  4. {
  5.         const char* p1 = str;
  6.         for( const char* p2; (p2=strchr(p1,chr))!=0; p1=p2+1 )
  7.                 printf( "\"%.*s\"\n", p2-p1, p1 );
  8.         printf( "\"%s\"\n", p1 );
  9. }

  10. int main( void )
  11. {
  12.         split001( "11|22||44", '|' );
  13.         return 0;
  14. }
复制代码

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
3 [报告]
发表于 2013-07-31 16:16 |只看该作者
  1. /* strtok.c [cobras] */

  2. #include <string.h>

  3. char *strtok_new(char *s, const char *delmt)
  4. {
  5.         static int str_next = 0;
  6.         static int str_len = 0;
  7.         char *p;
  8.         int i;

  9.         if (delmt != NULL) {
  10.                 for (i = 0; s[i] != '\0'; ++i) {
  11.                         if (strchr(delmt, s[i]) != NULL) {
  12.                                 s[i] = '\0';
  13.                         }
  14.                 }
  15.                 str_next = 0;
  16.                 str_len = i;
  17.                 return s;
  18.         }else {
  19.                 if (str_next >= str_len) {
  20.                         return NULL;
  21.                 }
  22.                 p = s + str_next;
  23.                 str_next += strlen(p) + 1;
  24.                 return p;
  25.         }
  26. }

  27. #include <stdio.h>

  28. int main(void)
  29. {
  30.         char text[] = "11|22||44";
  31.         char *p;

  32.         strtok_new(text, "|");
  33.         while ((p = strtok_new(text, NULL)) != NULL) {
  34.                 puts(p);
  35.         }
  36.         return 0;
  37. }
复制代码

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
4 [报告]
发表于 2013-07-31 16:35 |只看该作者
  1. NAME
  2.        strsep - extract token from string

  3. SYNOPSIS
  4.        #include <string.h>

  5.        char *strsep(char **stringp, const char *delim);

  6. DESCRIPTION
  7.        If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp, where
  8.        tokens are delimited by symbols in the string delim.  This token is terminated with a '\0' character (by overwriting the delimiter) and *stringp is updated
  9.        to point past the token.  In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

  10. RETURN VALUE
  11.        The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.

  12. NOTES
  13.        The  strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields.  However, strtok() conforms to ANSI-C and
  14.        hence is more portable.
复制代码

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
5 [报告]
发表于 2013-07-31 21:58 |只看该作者
int main( int argv, char * argc )

论坛徽章:
0
6 [报告]
发表于 2013-08-01 03:11 |只看该作者
本帖最后由 _码狗_ 于 2013-08-01 03:12 编辑
  1. #include <string.h>
  2. #include <stdio.h>

  3. char *my_strtok(char *string, const char *delimiter) {
  4.     static char *token_end;
  5.     static unsigned int delimiter_length;
  6.     char *token_start;
  7.     if (string) {
  8.         token_end = string;
  9.         delimiter_length = 0;
  10.         token_start = my_strtok(NULL, delimiter);
  11.     } else {
  12.         if (token_end) {
  13.             token_start = token_end + delimiter_length;
  14.             token_end = strstr(token_start, delimiter);
  15.             if (token_end) {
  16.                 *token_end = '\0';
  17.                 delimiter_length = strlen(delimiter);
  18.             }
  19.         } else {
  20.             token_start = NULL;
  21.         }
  22.     }
  23.     return token_start;
  24. }

  25. int main(void) {
  26.     char text[] = "11|22||44";
  27.     char *p;
  28.     p = my_strtok(text, "|");
  29.     while (p) {
  30.         puts(p);
  31.         p = my_strtok(NULL, "|");
  32.     };
  33.     return 0;
  34. }
复制代码

论坛徽章:
0
7 [报告]
发表于 2013-08-01 11:17 |只看该作者
自己写个就行了,用状态机什么的很快。 另外你可以输出答案?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP