-1

I need a function that works in C like split(' ') in python. I have to add words from a string as array elements (so I have to "cut" the string at every space characters and put the words in an array). I have no idea how to do it simply.

I tried for example the strstr() but it's just find the first space in the sentence, but I have 3 spaces.

Tilcsa220
  • 3
  • 1
  • What is your design for the function? What are the problems you're facing in the implementation of it? How will the calling code know how many words there are in the result? How will the calling code release the allocated space? Will you leave the input string unchanged or will you destroy it? Do adjacent separators separate empty strings or can you have any number of separators between strings? – Jonathan Leffler Nov 23 '22 at 16:22
  • There isn't a standard C function that does this job. I don't think there's a function to do it in POSIX either. So you will either roll your own or find one you can use on your system. It's not particularly hard to write, but it does require attention to the questions I asked. – Jonathan Leffler Nov 23 '22 at 16:27
  • See [this programming notes chapter](https://www.eskimo.com/~scs/cclass/notes/sx10h.html). (Also [this Stack Overflow question](https://stackoverflow.com/questions/49372173).) – Steve Summit Nov 23 '22 at 17:14

1 Answers1

0

strtok

"String tokenize"

int main ()
{
  char str[] ="one two three";
  char * word;
  word = strtok (str," ");
  while (word != NULL)
  {
    printf ("%s\n",word);
    word = strtok (NULL, " ");
  }
  return 0;
}

Be careful though, it modifies the input string. It replaces any "delimiter" chars with \0s.

It's also non-reentrant. It keeps state in some global variable so you can't interweave tokenizations of 2 different strings, not even talking about multi-threading.

Agent_L
  • 4,960
  • 28
  • 30
  • But there's `strtok_r()` on POSIX systems and `strtok_s()` on Windows, which have the same interface as each other and which provide both thread-safety and reentrancy. (Their interface is different from the interface to `strtok_s()` defined by the C standard in the optional but normative Annex K — a variation that almost no-one implements). – Jonathan Leffler Nov 23 '22 at 16:30
  • @JonathanLeffler Sure. But one has to start **somewhere** and the plain old `strtok` is THE place to start. Often, it suffices. When it doesn't suffice anymore, it's easier to understand replacements when you understand what are they replacing. – Agent_L Nov 23 '22 at 16:35
  • I'm more prejudiced against `strtok()` than you are. – Jonathan Leffler Nov 23 '22 at 16:36
  • @JonathanLeffler It's awful, I agree. But I assume that people who write production code know why to avoid `strtok` and people who don't know about `strtok` don't write production code. – Agent_L Nov 23 '22 at 16:38