1

I have this string: "apples,cakes,cupcakes,bannanas"

How can I efficiently break this up into an array like the following?

["apples"]["cakes"]["cupcakes"]["bannanas"]

There seem to be a lot of answers for c++ out there but I am struggling to find a answer for C. All i want to do is split this into a array at every ','. Any suggestions??

ThatPixelCherry
  • 323
  • 1
  • 4
  • 17
  • The easy way is [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) on an input stream. Read up on [`std::stringstream`](http://en.cppreference.com/w/cpp/io/basic_stringstream) for a quick way to turn a string into an input stream. – user4581301 Jan 20 '16 at 04:59
  • I thought std was a c++ library. Sorry, I am new to c and c++. – ThatPixelCherry Jan 20 '16 at 05:01
  • 1
    You tagged the question with C++, I gave a C++ answer. If you need to restrict your answers to C, edit your question and replace the C++ tag with the C tag. You will probably get a better quality of response. – user4581301 Jan 20 '16 at 05:05
  • Sorry im coding with c++ but using c strings. – ThatPixelCherry Jan 20 '16 at 05:10

2 Answers2

3

use strtok()?

string str as apples,cakes,cupcakes,bannanas and delim ",".

char *token;
token = strtok(str, delim);
while(token != NULL)
{
       printf("%s\n", token);
       token = strtok(NULL,delim);
 }

may this help.

Steve YonG
  • 80
  • 7
  • How does this return an array on strings??? I need a array with each split string in it. – ThatPixelCherry Jan 20 '16 at 04:59
  • Replace the print line with a strcpy of the `char` array into the string array. Or better still, use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – user4581301 Jan 20 '16 at 05:03
  • Thanks!! I just pushed the token into a vector :D – ThatPixelCherry Jan 20 '16 at 05:04
  • Two things to watch out for: 1. `strtok` destroys your source string. This is OK if you aren't using the string again and you aren't using it on a constant string with the assistance of a cast. 2. If you place the `char *` returned from `strtok` into the `std::vector` without copying the string at the `char *` into a `char` array in the `std::vector`, the `std::vector` relies on the continued existence of the tokenized string. If this string goes out of scope, all the pointers in the `std::vector` will refer to garbage and further use will result in unpredictable and undefined behaviour. – user4581301 Jan 20 '16 at 05:47
  • Too late to edit my previous comment. "strtok destroys your source string." is really bad wording. The string isn't destroyed in the destructor sense. The contents are changed. In this case every time strtok encounters a `','`, the `','` is replaced with a null. Attempts to read from the source string will terminate at the end of the first token because it is now null terminated. – user4581301 Jan 20 '16 at 06:26
1

You have several options.

You can use strtok function See documentation for strtok here

Similar question was asked before here

Community
  • 1
  • 1
Srini
  • 1,014
  • 8
  • 9