tags:

views:

306

answers:

4

Hi

How can i split a string into tokens by '&' ?

+5  A: 

strtok / strtok_r

char *token;
char *state;
for (token = strtok_r(input, "&", &state);
     token != NULL;
     token = strtok_r(NULL, "&", &state))
{
    ...
}
R Samuel Klatchko
+1  A: 

You should use strsep() instead of strtok().

anthony
A: 

I would do it something like this (using strchr()):

#include <string.h>

char *data = "this&&that&other";
char *next;
char *curr = data;
while ((next = strchr(curr, '&')) != NULL) {
    /* process curr to next-1 */
    curr = next + 1;
}
/* process the remaining string (the last token) */

strchr(const char *s, int c) returns a pointer to the next location of c in s, or NULL if c isn't found in s.

You might be able to use strtok(), however, I don't like strtok(), because:

  • it modifies the string being tokenized, so it doesn't work for literal strings, or is not very useful when you want to keep the string for other purposes. In that case, you must copy the string to a temporary first.
  • it merges adjacent delimiters, so if your string was "a&&b&c", the returned tokens are "a", "b", and "c". Note that there is no empty token after "a".
  • it is not thread-safe.
Alok
I suppose it also depends on the C implementation. On my system the string itself is not modified when I call strtok(). Actually I do not even see how it could. After all it just has to produce pointers to the start of the different tokens within the string.
Cees Meijer
Cees Meijer: `strtok()` *does* modify the argument string - the delimiter characters are replaced by '\0', so that the strings returned are properly terminated.
caf
`strtok` *has* to modify the string if it has to be standards compliant. From the C standard: *If such a character is found, it is overwritten by a null character, which terminates the current token*. This is very explicit. http://opengroup.org/onlinepubs/009695399/functions/strtok.html. Can you post code where `strtok()` doesn't modify the string?
Alok
Oops. I suppose you must be right. My mistake. Indeed changing the delimiter to \0 is the only way it could work. And after closer examination of my code (it's an embedded system so inspecting the disassembled code was not that hard) I see this is exactly what happens.
Cees Meijer
A: 

You can use the strok() function as shown in the example below.

/// Function to parse a string in separate tokens 

int parse_string(char pInputString[MAX_STRING_LENGTH],char *Delimiter,
                   char *pToken[MAX_TOKENS])
{
  int i;
  i = 0;

  pToken[i] = strtok(pInputString, Delimiter);
  i++;

  while ((pToken[i] = strtok(NULL, Delimiter)) != NULL){
     i++;
  }
  return i;
}

/// The array pTokens[] now contains the pointers to the start of each token in the (unchanged) original string.

sprintf(String,"Token1&Token2");
NrOfParameters = parse_string(String,"&",pTokens);

sprintf("%s, %s",pToken[0],pToken[1]);
Cees Meijer