Monday, 15 February 2010

c++ - How to split words by strtok function if the spaces are not equal -



c++ - How to split words by strtok function if the spaces are not equal -

suppose have split string words, (i.e. "i mamun") [here spaces not equal] have used 1 space delimiter in strtok function got wrong output. please explain :(

#include <stdio.h> #include <string.h> int main () { char str[] ="# timestep no_moles no_specs co3 co2 ho cho2 o cho3"; char * pch; printf ("splitting string \"%s\" tokens:\n",str); pch = strtok (str," #"); while (pch != null) { printf ("%s\n",pch); pch = strtok (null, " #"); } homecoming 0; }

my code : http://codepad.org/erwudkvh

the problem isn't different numbers of spaces, it's later fields delimited tab not space. changing

pch = strtok (null, " #");

to

pch = strtok (null, " \t#"); ^^

solves problem.

c++

No comments:

Post a Comment