How to get to the segmentation
My demand is to get the length after removing | like see now have 4
Then separately for content 1 2 3 content 4 content
CodePudding user response:
Using two-dimensional arrayRead to save it to a string array | stops,
Then read to save to the next member of the two-dimensional array
Cycle can be
CodePudding user response:
For teaching. The novice not!CodePudding user response:
Fyi:# include & lt; stdio.h>
#include
Char string [80].
Char seps1 [3].
Char seps2 [3].
Char * token;
Char * zzstrtok (
Char * string,
Const char * control1,//continuous emerged as empty token between
Const char * control2//continuous emerged as intermediate no empty 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].
The static char * nextoken;
The static char flag=0;
Unsigned char c;
Int L;
Memset (map1, 0, 32);
Memset (map2, 0, 32);
Do {
Map1 [3] * ctrl1 & gt; & gt; |=(1 & lt; & lt; (* ctrl1 & amp; 7));
} while (* ctrl1 + +);
Do {
Map2 [3] * ctrl2 & gt; & gt; |=(1 & lt; & lt; (* ctrl2 & amp; 7));
} while (* ctrl2 + +);
If (string) {
If (control2 [0]) {
L=strlen (string);
While (1) {
C=string [L - 1);
If (map2 [c & gt; & gt; 3) & amp; (1 & lt; & lt; (c & amp; 7))) {
L -;
String [L]=0;
} the else break;
}
}
If (control1 [0]) {
L=strlen (string);
C=string [L - 1);
If (map1 [c & gt; & gt; 3) & amp; (1 & lt; & lt; (c & amp; 7))) {
String [L]=control1 [0];
String [m + 1)=0;
}
}
String STR=(unsigned char *);
}
The else STR=nextoken (unsigned char *);
String=STR (char *);
While (1) {
If (0==flag) {
if (! * STR) break;
If (map1 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
* STR=0;
Str++;
break;
} else if (map2 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
String++;
Str++;
} else {
flag=1;
Str++;
}
{} else if (1==flag)
if (! * STR) break;
If (map1 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
* STR=0;
Str++;
Flag=0;
break;
} else if (map2 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
* STR=0;
Str++;
Flag=2;
break;
} the else str++;
} else {//2==flag
if (! * STR) return NULL;
If (map1 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
Str++;
String=STR (char *);
Flag=0;
} else if (map2 [* STR & gt; & gt; 3) & amp; (1 & lt; & lt; (* STR & amp; 7))) {
Str++;
String=STR (char *);
} else {
String=STR (char *);
Str++;
flag=1;
}
}
}
Nextoken=STR (char *);
If (string==(char *) STR) return NULL;
The else return a string;
}
Void main ()
{
Strcpy (string, "A \ \ tstring \ t tof,, tokens \ n \ nand more tokens,");
Strcpy (seps1, "\ n"); Strcpy (seps2, "\ t");
Printf (" \ n \ [% s] nTokens: \ n ", string);
Token=zzstrtok (string, seps1 seps2);
While (token!=NULL) {
Printf (" & lt; % s>" The 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 (" & lt; % s>" The 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 (" & lt; % s>" The 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 (" & lt; % s>" The 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 (" & lt; % s>" The 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 (" & lt; % s>" The 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 (" & lt; % s>" The token);
Token=zzstrtok (NULL, seps1 seps2);
}
Strcpy (string, "and a");
Strcpy (seps1, ", "); Strcpy (seps2, "");
Printf (" \ n \ [% s] nTokens: \ n ", string);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull