0

Possible Duplicate:
How to split a string in C++?

I have:

char[20] c = "@aaa,@bbb,@ccc";

and I need to get d such that:

d[0] ="@aaa" 
d[1] ="@bbb"
d[2] ="@ccc"

is there a function that does that? or I should write the function manually?

thanks in advance :)

Community
  • 1
  • 1
kakush
  • 3,334
  • 14
  • 47
  • 68
  • 1
    I saw that post, but in my case - I dont have a string.. I have char* , and if possible, I would like not to convert it to string.. – kakush Jun 06 '12 at 15:06
  • 2
    Well, if you want low-level C-strings, there's always `strtok()` ([docs](http://www.cplusplus.com/reference/clibrary/cstring/strtok/)) but *forget I mentioned it*, it's PURE EVIL ;-) Probably best to write your own, it's really not that hard... – Cameron Jun 06 '12 at 15:07
  • @Cameron - why evil? it sounds like exactly what i need – kakush Jun 06 '12 at 15:09
  • 1
    @kakush If you want to leave it as `char*` then that's fine, but the C++ string type exists exactly to make this kind of thing easier (as well as automatically de-allocate memory, etc.). If you want to build your own wheel then that's fine... but if your goal is to accomplish a task then it would be a good idea to use existing tools that will make the task easier. – cdhowie Jun 06 '12 at 15:09
  • 1
    @kakush It's evil because it cannot process two strings in parallel. *"On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. **In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning**."* That should scare you. – cdhowie Jun 06 '12 at 15:10
  • @kakush: Yes, as cdhowie said, it maintains global internal state between calls... which makes it really ugly, and prone to any problem where two parts of your code want to use it at once (in particular, multi-threaded parts, but also if you have e.g. nested string tokenization). It also means you can't change your string between calls, etc. – Cameron Jun 06 '12 at 15:13
  • @Mark: Sorry, I deleted my comment almost immediately, but it looks like you came to this question just when it was visible :) – Gorpik Jun 06 '12 at 15:21

0 Answers0