分割函数C语言 c语言split 分割函数

C语言如何分割字符串

可以写一个分割函数,用于分割指令,比如cat a.c最后会被分割成cat和a.c两个字符串、mv a.c b.c最后会被分割成mv和a.c和b.c三个字符串。

成都做网站、成都网站建设中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联专业成都网站建设10余年了,客户满意度97.8%,欢迎成都创新互联客户联系。

参考代码如下:

#include stdio.h

#includestring.h

#define MAX_LEN 128

void main()

{

int i,length,ct=0,start = -1;

char inputBuffer[MAX_LEN],*args[MAX_LEN];

strcpy(inputBuffer,"mv a.c b.c");

length=strlen(inputBuffer);

for (i = 0; i = length; i++) {

switch (inputBuffer[i]){

case ' ':

case '\t' :               /* argument separators */

if(start != -1){

args[ct] = inputBuffer;    /* set up pointer */

ct++;

}

inputBuffer[i] = '\0'; /* add a null char; make a C string */

start = -1;

break;

case '\0':                 /* should be the final char examined */

if (start != -1){

args[ct] = inputBuffer;

ct++;

}

inputBuffer[i] = '\0';

args[ct] = NULL; /* no more arguments to this command */

break;

default :             /* some other character */

if (start == -1)

start = i;

}

}

printf("分解之后的字符串为:\n");

for(i=0;ict;i++)

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

}

C语言中字符切割函数split的实现

#include stdio.h

#include string.h

// 将str字符以spl分割,存于dst中,并返回子字符串数量

int split(char dst[][80], char* str, const char* spl)

{

int n = 0;

char *result = NULL;

result = strtok(str, spl);

while( result != NULL )

{

strcpy(dst[n++], result);

result = strtok(NULL, spl);

}

return n;

}

int main()

{

char str[] = "what is you name?";

char dst[10][80];

int cnt = split(dst, str, " ");

for (int i = 0; i  cnt; i++)

puts(dst[i]);

return 0;

}

C语言怎么把字符串按行分割

int split(char dst[][80], char* str, const char* spl)

{

int n = 0;

char *result = NULL;

result = strtok(str, spl);

while( result != NULL )

{

strcpy(dst[n++], result);

result = strtok(NULL, spl);

}

return n;

}

int _tmain(int argc, _TCHAR* argv[])

{

char str[] = "123,456\n789,321";

char dst[10][80];

int cnt = split(dst, str, "\n");

for (int i = 0; i cnt; i++)

puts(dst[i]);

return 0;

}

主要是字符串分割函数strtok的使用

C语言有没有把字符串拆分为数组的函数?

用strtok函数实现吧。

void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现

{

char *s =NULL;

s=strtok(str,del);

while(s != NULL)

{

*arr++ = s;

s = strtok(NULL,del);

}

}

int main()

{

int i;

char *myArray[4];

char s[] = "张三$|男$|济南$|大专学历$|";

memset(myArray, 0x0, sizeof(myArray));

split(myArray, s, "$|");

for (i=0; i4; i++)

{

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

}

return 0;

}


分享标题:分割函数C语言 c语言split 分割函数
新闻来源:http://pwwzsj.com/article/dosijij.html