c语言中字母怎么分开,c语言拆分字符串,该怎么解决

C/C++ code#include

#include

char string[80];

char seps1[3];

char seps2[3];

char *token;

char *zzstrtok (

char *string,

const char *control1,//连续出现时视为中间夹空token

const char *control2 //连续出现时视为中间无空token

)

{

unsigned char *str;

const unsigned char *ctrl1 = (const unsigned char *)control1;

const unsigned char *ctrl2 = (const unsigned char *)control2;

unsigned char map1[32],map2[32];

static char *nextoken;

static char flag=0;

unsigned char c;

int L;

memset(map1,0,32);

memset(map2,0,32);

do {

map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));

} while (*ctrl1++);

do {

map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));

} while (*ctrl2++);

if (string) {

if (control2[0]) {

L=strlen(string);

while (1) {

c=string[L-1];

if (map2[c >> 3] & (1 << (c & 7))) {

L--;

string[L]=0;

} else break;

}

}

if (control1[0]) {

L=strlen(string);

c=string[L-1];

if (map1[c >> 3] & (1 << (c & 7))) {

string[L]=control1[0];

string[L+1]=0;

}

}

str=(unsigned char *)string;

}

else str=(unsigned char *)nextoken;

string=(char *)str;

while (1) {

if (0==flag) {

if (!*str) break;

if (map1[*str >> 3] & (1 << (*str & 7))) {

*str=0;

str++;

break;

} else if (map2[*str >> 3] & (1 << (*str & 7))) {

string++;

str++;

} else {

flag=1;

str++;

}

} else if (1==flag) {

if (!*str) break;

if (map1[*str >> 3] & (1 << (*str & 7))) {

*str=0;

str++;

flag=0;

break;

} else if (map2[*str >> 3] & (1 << (*str & 7))) {

*str=0;

str++;

flag=2;

break;

} else str++;

} else {//2==flag

if (!*str) return NULL;

if (map1[*str >> 3] & (1 << (*str & 7))) {

str++;

string=(char *)str;

flag=0;

} else if (map2[*str >> 3] & (1 << (*str & 7))) {

str++;

string=(char *)str;

} else {

string=(char *)str;

str++;

flag=1;

}

}

}

nextoken=(char *)str;

if (string==(char *)str) return NULL;

else return string;

}

void main()

{

strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");

strcpy(seps1,",\n");strcpy(seps2," \t");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");

strcpy(seps1,"|");strcpy(seps2," ");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"1234|LIYI||010|201110260000|OK");

strcpy(seps1,"");strcpy(seps2,"|");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"1234|LIYI||010|201110260000|OK");

strcpy(seps1,"|");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"a");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"a,b");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"a,,b");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",a");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,"a,");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",a,,b");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",,a,,b,,");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",,");

strcpy(seps1,",");strcpy(seps2,"");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

strcpy(string,",,,");

strcpy(seps1,",");strcpy(seps2," ");

printf("\n[%s]\nTokens:\n",string);

token=zzstrtok(string,seps1,seps2);

while (token!=NULL) {

printf(" ",token);

token=zzstrtok(NULL,seps1,seps2);

}

}

//

//[A string of ,,tokens

//

//and some more tokens,]

//Tokens:

// , , , <>, , <>, , , , , <>,

//[1234| LIYI|China | 010 |201110260000|OK]

//Tokens:

// <1234>, , , <010>, <201110260000>, ,

//[1234|LIYI||010|201110260000|OK]

//Tokens:

// <1234>, , <010>, <201110260000>, ,

//[1234|LIYI||010|201110260000|OK]

//Tokens:

// <1234>, , <>, <010>, <201110260000>, ,

//[a]

//Tokens:

// ,

//[a,b]

//Tokens:

// , ,

//[a,,b]

//Tokens:

// , <>, ,

//[,a]

//Tokens:

// <>, ,

//[a,]

//Tokens:

// , <>,

//[,a,,b]

//Tokens:

// <>, , <>, ,

//[,,a,,b,,]

//Tokens:

// <>, <>, , <>, , <>, <>,

//[,]

//Tokens:

// <>, <>,

//[,,]

//Tokens:

// <>, <>, <>,

//[,,,]

//Tokens:

// <>, <>, <>, <>,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值