1

I have array[2][3]= {e-> id = m, m-> t|e|null};

I am trying to split the string in both ways: if it founds ' " " ' (empty space) it should split and if it founds "|" then it also again splits. I know this is not correct but any one who can help me?

for i=0 t0 row 
   for j=0 to col
      c41= a[i][j].c_str();
      strcpy(pch55,c41);
      pch5=strtok(pch55,"|" || " "); // is it correct???
      for ( int u=0;pch5 != NULL;u++)
      {
          z33[u]= pch5;

          pch5 = strtok (NULL,"|" || " ");  //is it correct??

      }
giantmalik
  • 137
  • 1
  • 7
  • 1
    What kind of C++ is this? That preamble looks like pseudo-code. Using `strtok` is also C, not C++. – tadman Jan 06 '15 at 17:24
  • @tadman: The entire C string library is available in C++. So `strtok` is C++. – Thomas Matthews Jan 06 '15 at 17:43
  • I caution you against using `strtok`, as it modifies your string. If possible, switch over to `std::string` and use the `find*` methods. – Thomas Matthews Jan 06 '15 at 17:44
  • i am trying when space or "|" comes it shd split the string so how should i do? – giantmalik Jan 06 '15 at 17:45
  • "Splitting" a string may also be known as creating a *substring* which is often abbreviated as `substr` (hint). – Thomas Matthews Jan 06 '15 at 17:46
  • @ThomasMatthews That would be C by definition, since it's the C library. That C and C++ are inter-operable is simply a feature. There's nothing in this question that is C++-specific. – tadman Jan 06 '15 at 17:50
  • `array[2][3]= {e-> id = m, m-> t|e|null};`? What even is that? `for i=0 t0 row` is invalid, as is `for j=0 to col`, at least in C++. Looks more like BASIC or something. `strcpy(...)` and following are not inside the loop like they're maybe supposed to be guessing by indentation - this isn't Python. None of the variables are declared, so their types are completely non-obvious. `strtok(pch55, "|" || " ")` is equivalent to `strtok(pch55, "|")` - the logical OR with space there is completely pointless. So much more... – twalberg Jan 06 '15 at 18:05
  • @tadman: Did you catch the statement `c41= a[i][j].c_str();` in the code? My understanding is `.c_str()` is not a C language method, but usually the method invocation from `std::string`. – Thomas Matthews Jan 06 '15 at 18:10
  • Good observation. That was lost in the rather disastrous formatting there. Found [this example](http://stackoverflow.com/questions/289347/using-strtok-with-a-stdstring) with some suggestions on how to do this. – tadman Jan 06 '15 at 18:15

1 Answers1

1

Use

pch5=strtok(pch55,"| "); // notice the space at the end

on line 5, and

pch5=strtok(NULL, "| ");

on line 10.

The 2nd arg of strtok(3) is a collection of characters to use as delimiters, not a string delimiter. And, the NULL tells it to continue from where it left off.

Danny Daglas
  • 1,501
  • 1
  • 9
  • 9
  • its not working remember my array has a[2][3]={e-> id = m, m-> t|e|null}; and i want to split when space or "|" comes. – giantmalik Jan 06 '15 at 17:36