tokenizing a string in C -
Hello I want to make a string to a token in C
The following string is.
<{Job Running} {Job Running} {Job Running} {Job Finite}I want to token to {and} to give me "job start", "job running" and "Job is over"
I also want to use the same delimiter as the characters to run away
{started the job} {work {ID1}} is going on {Work {ID2}}} {Job Finished}
I should start the job
job, {id1} running, job (id2) running Is the job It went to be received.
I have the solution Pointer Air-Metmatic, but do not want it to iterate over the input string more than once.
Any suggestions.
You can use a simple finite state machine:
#include & lt; Stdio.h & gt; Int main () {char * src = "{initial}} {job} \ {ID1 \\} running} {job} \ {id2 \\} running} {job expired}"; Four tokens [100] = {}, * dst = tokens, ch; Int State = 0; Switch case (case:) (case =}} ==} {switch (state) {case 0: if (ch == '{'} state = 1; breakage; case 1: switch (ca) {case '}' : Printf ("token:% s \ n", token); dst = token; * dst = 0; state = 0; breakage; case '\\': state = 2; break; default: * dst ++ = ch ; * DST = 0;} break; Case 2: * dst ++ = ch; * DST = 0; state = 1; break;}}}
Comments
Post a Comment