亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5522 | 回復(fù): 6
打印 上一主題 下一主題

[C] strtok分割字符串的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-07-31 15:43 |只看該作者 |倒序?yàn)g覽
輸入: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. }
復(fù)制代碼
這段代碼沒有什么問題,
但是如果輸入里有一項是空的時候:例如  11|22||44
這時候就只能得到 11   22  44
而我想要的是還是得到4個結(jié)果  只是第三個是空的就可以了

不知道這個可以這么實(shí)現(xiàn)呢?

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期: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技術(shù)圖書徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [報告]
發(fā)表于 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. }
復(fù)制代碼

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
3 [報告]
發(fā)表于 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. }
復(fù)制代碼

論壇徽章:
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 [報告]
發(fā)表于 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.
復(fù)制代碼

論壇徽章:
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 [報告]
發(fā)表于 2013-07-31 21:58 |只看該作者
int main( int argv, char * argc )

論壇徽章:
0
6 [報告]
發(fā)表于 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. }
復(fù)制代碼

論壇徽章:
0
7 [報告]
發(fā)表于 2013-08-01 11:17 |只看該作者
自己寫個就行了,用狀態(tài)機(jī)什么的很快。 另外你可以輸出答案?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP