Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string into tokens in C?

Tags:

c

split

token

How to split a string into tokens by '&' in C?

like image 238
trrrrrrm Avatar asked Jan 19 '10 07:01

trrrrrrm


3 Answers

strtok / strtok_r

char *token;
char *state;

for (token = strtok_r(input, "&", &state);
     token != NULL;
     token = strtok_r(NULL, "&", &state))
{
    ...
}
like image 150
R Samuel Klatchko Avatar answered Oct 22 '22 08:10

R Samuel Klatchko


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.
like image 20
Alok Singhal Avatar answered Oct 22 '22 08:10

Alok Singhal


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]);
like image 2
Cees Meijer Avatar answered Oct 22 '22 06:10

Cees Meijer