您的位置:首页 > 其它

C 语言 分割字符串 strtok函数

2013-05-21 10:00 447 查看
#include <stdio.h>

#include <string.h>

char name[20];

int split_string(char input[], char output[])

{

char *p;

int i=0;

const char *split="=";

memset(output, 0, sizeof(output));

p = strtok(input, split);

while(p)

{

if(i ==0 )

{

strcpy(output, p);

printf("%s\n", name);

}

else

{

strcpy(output, p);

printf("%s\n", output);

}

i++;

p=strtok(NULL, split);

}

return 0;

}

int main(int argc, char ** argv)

{

char a[16];

char buff[16];

strcpy(a, argv[1]);

split_string(a, buff);

return 0;

}

//==========================================================================

#include <stdio.h>

#include <string.h>

char name[20];

int split_string(char input[], char output[6][2])

{

char *p;

int i=0;

const char *split=":";

p = strtok(input, split);

while(p)

{

strcpy(output[i], p);

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

i++;

//printf("%s\n",p);

p=strtok(NULL, split);

}

return 0;

}

int main()

{

char a[20];

char buff[6][2];

char data[]="11:22:33:44:55:66";

strcpy(a, data);

split_string(a, buff);

return 0;

}

//==========================================================================

#include <stdio.h>

#include <string.h>

int split_string()

{

int i=0;

const char *split="\n";

char buff[256]="hello\nwork";

char *p;

char first[32],second[32];

memset(&buff,0,sizeof(buff));

p = strtok(temp,split);

while(p)

{

if(i == 0)

strcpy(first,p);

else if(i == 1)

strcpy(second,p);

i++;

p=strtok(NULL,split);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: