首页 > 编程知识 正文

关于c语言strtok函数用法下划线分隔的信息

时间:2024-03-25 09:49:56 阅读:332461 作者:OMES

本文目录一览:

c++里面,函数strtok怎么用?

strtok:

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。

功能:

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。

例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。

函数使用:

strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。

c

#includestring.h

#includestdio.h

int main(void)

{

char input[16]="abc,d";

char*p;

/*strtok places a NULL terminator

infront of the token,if found*/

p=strtok(input,",");

if(p)

printf("%sn",p);

/*Asecond call to strtok using a NULL

as the first parameter returns a pointer

to the character following the token*/

p=strtok(NULL,",");

if(p)

printf("%sn",p);

return0;

}

c++

#includeiostream

#includecstring

using namespace std;

int main()

{

char sentence[]="This is a sentence with 7 tokens";

cout "The string to be tokenized is:n" sentence "nnThe tokens are:nn";

char *tokenPtr=strtok(sentence,"");

while(tokenPtr!=NULL) {

couttokenPtr'n';

tokenPtr=strtok(NULL,"");

}

//cout "After strtok,sentence=" tokenPtrendl;return0;

}

函数第一次调用需设置两个参数。第一次分割的结果,返回串中第一个 ',' 之前的字符串,也就是上面的程序第一次输出abc。

第二次调用该函数strtok(NULL,","),第一个参数设置为NULL。结果返回分割依据后面的字串,即第二次输出d。

strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置

线程安全的函数叫strtok_r,ca

运用strtok来判断ip或者mac的时候务必要先用其他的方法判断'.'或':'的个数,因为用strtok截断的话,比如:"192..168.0...8..."这个字符串,strtok只会截取四次,中间的...无论多少都会被当作一个key

其他相关信息

下面的说明摘自于最新的Linux内核2.6.29,说明了这个函数已经不再使用,由速度更快的strsep()代替

/** linux/lib/string.c** Copyright (C) 1991, 1992 Linus Torvalds*//** stupid library routines.. The optimized versions should generally be found

* as inline code in asm-xx/string.h 

* These are buggy as well.. 

* * Fri Jun 25 1999, Ingo Oeser ioe@informatik.tu-chemnitz.de 

* - Added strsep() which will replace strtok() soon (because strsep() is 

* reentrant and should be faster). Use only strsep() in new code, please. 

** * Sat Feb 09 2002, Jason Thomas jason@topic.com.au, 

* Matthew Hawkins matt@mh.dropbear.id.au 

* - Kissed strtok() goodbye

*/

关于c语言字符串中切割函数strtok的用法

strtok()函数并不像你想的那样可以一次切割字串。需要多次循环,第二次时需要用 p = strtok(NULL, " "); 这样的 形式。

void main()

{ char test1[] = "Hello C World";

char *p;

p = strtok(test1, " ");

while(p)

{

printf("%sn", p);

p = strtok(NULL, " ");

}

return 0;

}

运行结果:

Hello

C

World

我想用c语言中的strtok函数得到一个字符串中由分隔符分割的某些关键字,并处理

#include stdio.h

#include string.h

int main(void)

{

 char str[100], spl[10], *p;

 fputs("请输入字符串 : ", stdout);

 gets(str);

 fputs("请输入分割符 : ", stdout);

 gets(spl);

 p = strtok(str, spl);

 while (p != NULL)

 {

  puts(p);

  p = strtok(NULL, spl);

 }

 return 0;

}

请问,C语言中,对带分隔符的字符串如何分割?

C/C++中的Split函数是strtok()其函数原型如下:

char

*

strtok

(char

*

str,

const

char

*

delimiters);

函数说明

strtok()用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串,当strtok()在参数str的字符串中发现到参数delimiters的分割字符时则会将该字符改为''字符。在第一次调用时,strtok()必需给予参数str字符串,往后的调用则将参数str设置成NULL。每次调用成功则返回下一个分割后的字符串指针。

返回值

返回下一个分割后的字符串指针,如果已无从分割则返回NULL。

示例-1

/*

strtok

example

*/

#include

stdio.h

#include

string.h

int

main

()

{

char

str[]

="a,b,c,d*e";

const

char

*

split

=

",";

char

*

p;

p

=

strtok

(str,split);

while(p!=NULL)

{

printf

("%sn",p);

p

=

strtok(NULL,split);

}

getchar();

return

0;

}

本例中,实现对字符串'a,b,c,d*e"用逗号(,)来作界定符对字符串进行分割。

输出结果将如下所示:

a

b

c

d*e

因为delimiters支持多个分割符,

我们将本示例中的语句行

const

char

*

split

=

",";

改成

const

char

*

split

=

",*";

//用逗号(,)和星号(*)对字符串进行分割

这样输出结果将如下所示:

a

b

c

d

e

C语言中strtok用法

一般来说,条件关键词(if

else

else

if

for

while)只能作用于

紧随其后的

第一句

代码。

{

}的作用,你可以这么理解:是把‘被

括起来

的所有代码’

当成

‘一句代码’

送给关键词来处理。

注意:被括起来的可以是多句,当然也可以是一句哦。

if(a

==

b)

printf(

"a

==

b");

printf(

"a

!

a

!b

");

这个时候

第二个

printf

if

来说不是紧随的第一句所以不受if

限制。一定会输出。

if(a

==

b)

{

printf(

"a

==

b");

printf(

"a

!

a

!b

");

}

这个时候,整个大括号里的(两句

printf)就是

紧随

if

的第一句代码了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。