找回密码
 注册
关于网站域名变更的通知
查看: 300|回复: 1
打印 上一主题 下一主题

了解一下C语言字符串的拆分函数strtok

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-3-26 09:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

  X+ K/ p" l8 \! a# C/ ?char *  strtok ( char * string, const char * delimiters );4 d; Y$ K" H  G8 L3 k+ h: `

, j$ a  t5 u" O$ f8 bSequentially truncate string if delimiter is found.
# S0 c; `7 Y. O8 [, Q5 I! h! I  If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
' g0 S7 o0 [# o" P* v% ?  After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.4 @/ y3 v! D2 n! o6 h9 c1 n4 _5 N; R* x
  delimiters may vary from a call to another.& z' d5 n: @/ J" }$ u! [. \

, v9 F; w0 M: A  J5 w* w3 @Parameters.$ c/ Z) j4 Y% ^( C! Q
. F* L7 i! h' f- T! x/ n' V
string& m& }' D. [7 W& ~# G$ A
Null-terminated string to scan.) i8 i  i: H" F3 y
separator3 K$ \  P0 y# w6 e: Z# i2 b
Null-terminated string containing the separators.
+ D- R, C; f9 [5 aReturn Value.  b4 u% w+ Y( `' C; d( m
  A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.
. v8 m0 w* m8 L; f6 A/ x9 ?% [2 V5 O. V$ {
Portability.
6 C( E' G6 d9 R$ Y# @/ J! k) U' ]  Defined in ANSI-C.
3 f% i. C! W$ _' O. t0 g# G6 a; d3 i3 `
Example.- y# H+ x) c: U* ?& @# _+ N

( T' |  e% `8 J/* strtok example */
5 v0 ]3 N4 ^! b" L% s#include <stdio.h>4 c' ]& D! C, j7 }4 [+ M
#include <string.h>' T. O  B* R* o. E4 `) R

/ |/ I5 D4 x6 Gint main ()$ C4 I# J/ u6 d) q3 V& W
{
& S( y2 J% f8 C$ ]8 J' T/ `- ^: l  char str[] ="This is a sample string,just testing.";
$ l) d$ H& J- ?4 w4 R  char * pch;
/ b$ t! p) V% i4 p6 I2 K  printf ("Splitting string \"%s\" in tokens:\n",str);1 f3 ~' F, Z' U/ R
  pch = strtok (str," ");" `- N* f4 Q1 @
  while (pch != NULL)7 y. s+ Q1 i/ R" s+ f1 @! Q7 G. N
  {
# F( V: r, x' G; Y4 i' w    printf ("%s\n",pch);5 @4 t6 v& q* Z; P! p$ J# X
    pch = strtok (NULL, " ,.");. T% Y$ q1 \2 Z: ]# j
  }
! ?- V) f2 I( L0 C  return 0;$ Q, u% W4 y; Z  z) I
}
- R" t; L( g: ?2 X! O1 I0 R5 QOutput:
. s$ h% |" F) WSplitting string "This is a sample string,just testing." in tokens:' a  w/ X  g* j. D4 B4 L
This
, M8 |) l3 y* l! d) \! His! d. h8 p8 y  E; ~* Y! c
a+ Y/ m0 F" x. F9 O+ o: T
sample; y6 Q- S( R- a, j4 l. z/ [5 {
string7 h4 }' n( @# ]& D2 f: f
just' N( |; ]& o) I, ~! F9 W
testing
  • TA的每日心情

    2019-11-29 15:37
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2020-3-26 18:26 | 只看该作者
    了解一下C语言字符串的拆分函数strtok
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-25 17:54 , Processed in 0.140625 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表