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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
) n* u0 A/ G  g0 Q' Q+ r% O/ V: o
char *  strtok ( char * string, const char * delimiters );
, H$ X1 k2 F; _- o$ d5 Z% {8 E7 B" [
% x$ e5 N, b) T4 F6 ?( GSequentially truncate string if delimiter is found.
. X# A. d2 s" x: s. u: `9 p1 H4 ^6 C  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.- g9 {1 A* a9 W% J  w
  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.) S7 W' g- j) `9 _
  delimiters may vary from a call to another.
! W/ a; S/ i3 ?6 D* g8 _+ k& ^0 G. c( t" @3 O
Parameters., h' @( d5 H" n% }* j& _( ^

" z7 N8 q! r" ?string  ?  o: d/ J# C6 y$ B, n
Null-terminated string to scan.
% g7 t8 N% z- e" C, Cseparator& `+ J. P' M) x+ T* A  {& g
Null-terminated string containing the separators.
  m* R/ y1 c$ h- Q; X& ZReturn Value.
' T0 L" t, t0 t( l0 S; a  A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.7 H/ \* w3 d4 @) g& a

/ z, J% L! ~5 x( b& [( x! APortability.
/ \' C+ ~) r, }8 R3 h  Defined in ANSI-C.
. Z8 g& B# m7 J+ r+ {
+ Z! T# W9 {3 M" O% d8 bExample.
" h: k% m( V* q7 F  Q, |/ Y  p& f# l
/* strtok example */1 a% r' _2 x: O- b6 E; h3 X5 e  @
#include <stdio.h>0 n% k  B% v, d* J; A  n6 ~9 ]
#include <string.h>1 @6 W3 V: Z0 w9 M# B

: M2 s7 C" Y: J8 h0 @+ \% Fint main ()! ]+ G% P4 U) }3 P1 j5 p& t$ z
{. O' m! ]( }; ^& H1 K0 i+ L9 _
  char str[] ="This is a sample string,just testing.";
3 ]" U9 m/ t' D% l, N  Z  char * pch;
: Y+ D2 O9 V4 Z  printf ("Splitting string \"%s\" in tokens:\n",str);
+ Y$ c1 q2 a' l9 r  pch = strtok (str," ");3 r+ c- Z! Z% J" I  c
  while (pch != NULL)
! l, F$ }& w+ a& ]( g- w% Y  {1 |# ]- r6 B- S0 z1 l; g* Q4 [
    printf ("%s\n",pch);
* ?7 R' U$ A# ^( h1 c/ @    pch = strtok (NULL, " ,.");
8 X. |4 e& x( s  \  }
8 n& p( g1 V5 A) b) R# i! a; G! K5 p" k  return 0;3 F1 W) ]# f+ }! E
}
: r% h/ U' H2 s4 W, cOutput:- i! Y3 ]2 X9 H0 y  U  P& I
Splitting string "This is a sample string,just testing." in tokens:! f& Q/ c: \+ W0 ?# W1 X4 ]
This
% V6 S8 o. o4 M. u1 Yis
) q( d# x- O; W  X5 Y# a/ q9 Ia
8 h) k' `- x* r3 Gsample4 e% ]6 u* B# @9 }) y; g
string' g3 e1 |# r( k6 X5 U
just/ {" V3 |1 r  K" m  k
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 16:43 , Processed in 0.171875 second(s), 23 queries , Gzip On.

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

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

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