百度
360搜索
搜狗搜索

c语言strtok函数用法,C语言中strtok用法详细介绍

本文目录一览: C语言字符串处理函数strtok

#include

char *strtok( char *str1, const char *str2 ); 功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。

例如:

char str[] = "now # is the time for all # good men to come to the # aid of their country"; char delims[] = "#"; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); } 以上代码的运行结果是:

result is "now " result is " is the time for all " result is " good men to come to the " result is " aid of their country" 相关主题:

c语言怎么把一个字符数组分成多个数组?

#include

#include

#define SUB 2

static char a[8] = {1,2,3,4,5,6,7,8};

static char b0[SUB], b1[SUB], b2[SUB], b3[SUB];

static void split(char a[], char b[], int n)

{

int i;

for(i = 0; i < n; ++i)

{

a[i] = b[i];

}

}

int main(int argc, char *argv[])

{

split(a, b0, SUB);

split(a + SUB, b1, SUB);

split(a + (SUB << 1), b2, SUB);

split(a + (SUB << 1) + SUB, b3, SUB);

return 0;

}

#include

void main(){ unsigned char a[8]={1, 2, 3, 4, 5, 6, 7, 8}, char b[4][2]; for (i=0;i<4;i++) for (j=0;j<2;j++) for (k=0;k<8;k++) b[i][j]=a[k]; for (i=0;i<4;i++) for (j=0;j<2;j++) printf("%d/t", b[i][j]);}

b[0]=0x12;b[1]=0x34;b[2]=0x56;b[3]=0x78;

可以使用strtok函数把一个字符数组分解成多个字符数组。

1、strtok函数:原型:char *strtok(char s[], const char *delim);功能:将一个字符串分解为一组字符串,s为要分解的字符串,delim为分隔符字符串;说明:当strtok函数在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针;头文件:string.h;返回值:从字符串s开头开始的一个个被分割的字符串。分割结束时,返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。2、例程:

#include

#include

int main(){ char input[16]="abc,d,ef,g";//待分解字符串 char* delim=",";//分隔符字符串 char* p=strtok(input,delim);//第一次调用strtok while(p!=NULL){//当返回值不为NULL时,继续循环 printf("%s\n",p);//输出分解的字符串 p=strtok(NULL,delim);//继续调用strtok,分解剩下的字符串 } return 0;}

C语言中strtok用法

你的问题让我研究了一晚上,
也没弄明白,哎,
这个代码太神奇了。
不知道用什么标记了第一个字符指针的起始位置。
  strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。
  strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置
  线程安全的函数叫strtok_r,ca。
  运用strtok来判断ip或者mac的时候务必要先用其他的方法判断'.'或':'的个数,因为用strtok截断的话,比如:"192..168.0...8..."这个字符串,strtok只会截取四次,中间的...无论多少都会被当作一个key。
  函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。
  后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。
  如果调用strtok时已经没有标记,则strtok返回NULL。注意strtok修改输入字符串,因此,如果调用strtok之后还要在程序中使用这个字符串,则应复制这个字 符串。

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

可以定义一个数组,将找到数据存储到数组中,然后,再根据数组内容进行数据检查处理。
如:
char *ps[100]; //单词不要超过100个
int i=0,j;
char *t=strtok(str,c);
while( t )
{
ps[i++]=t;
t=strtok( NULL, c );
}
for( j=0;j<i ; j++ )
{
进行数据检查和处理
}
可以定义一个数组,将找到数据存储到数组中,然后,再根据数组内容进行数据检查处理。
如:
char *ps[100]; //单词不要超过100个
int i=0,j;
char *t=strtok(str,c);
while( t )
{
ps[i++]=t;
t=strtok( NULL, c );
}
for( j=0;j<i ; j++ )
{
进行数据检查和处理
}
原因是,strtok函数的分割符是单字节字符,而一个汉字是两个字节。所以,当分隔符为“的是”时实际上是指定了四个分隔符。
请采纳答案,支持我一下。
strtok()函数并不像你想的那样可以一次切割字串。需要多次循环,第二次时需要用 p = strtok(NULL, " "); 这样的 形式。
void main()
{ char test1[] = "Hello C World";
char *p;
p = strtok(test1, " ");
while(p)
{
printf("%s\n", p);
p = strtok(NULL, " ");
}
return 0;
}
运行结果:
Hello
C
World

阅读更多 >>>  strcat函数用法详解,strchr函数的用法

<i ; j++ )
<i ; j++ )
c语言中如何让输出的数值分段
<i ; j++ )
<i ; j++ )

进行数值分段主要进行字符串分割,使用strtok函数即可实现字符串分割。这里引用一段strtok用法:
The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.
For example:char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {undefined
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
/* 何问起 hovertree.com */
The above code will display the following output:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"

C语言中怎么把字符数组里的数据存放到字符串数组中

/*
C语言中怎么把字符数组里的数据存放到字符串数组中
*/
#include

#include

void main()

{

char a[ 100 ], *p, *q;

int i, j, n = 0;

char *b[ 50 ];//字符指针数组

printf("Please input a sentence:\n");

gets( a );

p = a;

while( *p != '\0' ) {

if( *p == ' ') {//跳过空格

p++;

continue;

}

else {

i = 0;

while( *( p + i ) != ' ' && *( p + i ) != '\0' )

i++;

q = new char[ i + 1 ];//创建一个新的字符数组,存储单词

for( j = 0; j < i; j++ )

q[ j ] = p[ j ];

q[ i ] = '\0';//记得放上结束符

b[ n++ ] = q;//用字符数组中的元素指向新创建的字符数组

p += i;

}

}

//以下换行输出,验证是否正确

for( i = 0; i < n; i++ )

printf( "%s\n", b[ i ] );

for( i = 0; i < n; i++ )

delete []b[i];//释放各个字符数组

}

//测试:输入

// sdkjf skdj d

// 输出

// sdkjf

// skdj

// d

P.S: 或者输入一个完整的英文,如 " I am a genius!" 也可。程序会自动忽略空格。:-) 用C的malloc和free写起来较长,喜欢用C++中的new和delete

利用C语言标准库中的strtok()函数,可以轻松快捷的将以空格分隔开的一串字符分成多个子串,存放到字符指针数组中。

函数说明:

#include

阅读更多 >>>  strcat函数的用法,matlab 中 strcat函数的用法

//头文件

char *strtok(char *str, const char *delim);

strtok()用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数str的字符串中发现参数delim中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数str字符串,往后的调用则将参数str设置成NULL。每次调用成功则返回指向被分割出片段的头指针。当没有被分割的串时则返回NULL。

参考代码:

c语言:如何把字符串分解为一个个的字符?

直接从字符数组中提取就可以啦!
例如:
char a[10]="abcd";
那么,从a[0]到a[3]分别是'a','b','c','d'四个字符。
void insert(char str[])
{
inti;
char *p;
p=s;
for(i=0;str[i]!='\0';i++);
*(p+2*i-1)='\0';
for( ;i>=2;i--)
{
*(p+2*i-2)=*(p+i-1);
*(p+2*i-3)=' ';
}
}
main()
{
char str[50];
gets(str);
insert(str);
puts(str);
}
1.
不需要专门分割,c语言里面本来就是用字符数组来保存的,如:char
a[20]="hello
world!";这个字符串,char[0]就是h,char[1]就是e。
2.
如果要分割子串,可以使用strtok函数。
char
*strtok(char
*s,
char
*delim);
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
首次调用时,s指向要分解的字符串,之后再次调用要把s设成null。
strtok在s中查找包含在delim中的字符并用null('')来替换,直到找遍整个字符串。

strtok((char )NULL, = ) 什么意思 C语言

msdn上的解释
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken.
就是说,第一次用一个字符串做第一个参数,以后用null做第一个参数就可以了。
http://baike.baidu.com/view/1028553.htm
原型:char *strtok(char *s, char *delim);
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。 strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。 返回指向下一个标记串。当没有标记串时则返回空字符NULL。

怎样理解C语言中string.h里strtok()函数??

网上找的资料,你看看
原型:char *strtok(char *s, char *delim);
功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点
1.#include

2.#include

3.int main(int argc,char **argv)

4.{

5.char * buf1="aaa, ,a, ,,,bbb-c,,,ee|abc";

6./* Establish string and get the first token: */

7.char* token = strtok( buf1, ",-|");

8.while( token != NULL )

9. {

10./* While there are tokens in "string" */

11. printf( "%s ", token );

12./* Get next token: */

13. token = strtok( NULL, ",-|");

14. }

15.return 0;

16.}

网站数据信息

"c语言strtok函数用法,C语言中strtok用法"浏览人数已经达到22次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:c语言strtok函数用法,C语言中strtok用法的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!