Using strtok in c
Asked Answered
O

7

17

I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays?

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="test string.";
  char * test;
  test = strtok (str," ");
  while (test != NULL)
  {
    printf ("%s\n",test);
    test= strtok (NULL, " ");
  }
  return 0;
}
Olly answered 12/11, 2011 at 18:57 Comment(5)
can I use those to read a char array until a space?Olly
No, I meant "use them in conjunction with strtok". I.e. copy the token (pointed to by test) into your target string.Ureter
@KerrekSB though using strchr and strndup would be faster and more flexible (no need to clobber the input)Aramen
@sehe: True. Many ways to skin this cat. The OP seems to have decided on strtok already, so I just went with it...Ureter
@KerrekSB: I added a strtok free way to skin this cat none-the-less. Removes all the problems associated with strtok.Aramen
A
19

Here is my take at a reasonably simple tokenize helper that

  • stores results in a dynamically growing array
  • null-terminating the array
  • keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a literal char[], at least I think in C99)

To make the code re-entrant, use the non-standard strtok_r

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char** tokenize(const char* input)
{
    char* str = strdup(input);
    int count = 0;
    int capacity = 10;
    char** result = malloc(capacity*sizeof(*result));

    char* tok=strtok(str," "); 

    while(1)
    {
        if (count >= capacity)
            result = realloc(result, (capacity*=2)*sizeof(*result));

        result[count++] = tok? strdup(tok) : tok;

        if (!tok) break;

        tok=strtok(NULL," ");
    } 

    free(str);
    return result;
}

int main ()
{
    char** tokens = tokenize("test string.");

    char** it;
    for(it=tokens; it && *it; ++it)
    {
        printf("%s\n", *it);
        free(*it);
    }

    free(tokens);
    return 0;
}

Here is a strtok-free reimplementation of that (uses strpbrk instead):

char** tokenize(const char* str)
{
    int count = 0;
    int capacity = 10;
    char** result = malloc(capacity*sizeof(*result));

    const char* e=str;

    if (e) do 
    {
        const char* s=e;
        e=strpbrk(s," ");

        if (count >= capacity)
            result = realloc(result, (capacity*=2)*sizeof(*result));

        result[count++] = e? strndup(s, e-s) : strdup(s);
    } while (e && *(++e));

    if (count >= capacity)
        result = realloc(result, (capacity+=1)*sizeof(*result));
    result[count++] = 0;

    return result;
}
Aramen answered 12/11, 2011 at 19:15 Comment(6)
I think the realloc line should have sizeof(*result), not sizeof(result), and the first argument should obviously be result and not realloc.Cole
Added a strtok-free version (that doesn't need to modify it's input, using strpbrk. This is going to be more efficient).Aramen
strdup + strndup are not C standard, also strtok_r is'nt, it's only POSIX.Lipman
@Lipman AFAICT both strdup and strtok_r are part of IEEE Std 1003.1,2004 Edition. Obviously, good point on the strndup but, it is easily wrapped (or just use the strtok version)Aramen
char str[] ="test string." here str is a char[13] and perfectly safe to modify. (It was unclear to me what you meant by "char[] literal")Cinch
@kaizer.se: it could be me confusing C99 and C++03 (I'm a C++ guy mostly)Aramen
C
7

Do you need to store them separately? Two pointers into a modified char array will yield two separate perfectly usable strings.

That is we transform this:

char str[] ="test string.";

Into this:

char str[] ="test\0string.";
             ^     ^
             |     |
char *s1 -----     |
char *s2 -----------

.

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="test string.";
  char *firstname = strtok(str, " ");
  char *lastname = strtok(NULL, " ");
  if (!lastname)
    lastname = "";
  printf("%s, %s\n", lastname, firstname);
  return 0;
}
Cinch answered 12/11, 2011 at 22:8 Comment(0)
E
4

What about using strcpy:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_NAMES 2

int main ()
{
  char str[] ="test string.";
  char *names[MAX_NAMES] = { 0 };
  char *test;
  int i = 0;

  test = strtok (str," ");
  while (test != NULL && i < MAX_NAMES)
  {
    names[i] = malloc(strlen(test)+1);
    strcpy(names[i++], test);
    test = strtok (NULL, " ");
  }

  for(i=0; i<MAX_NAMES; ++i)
  {
    if(names[i])
    {
      puts(names[i]);
      free(names[i]);
      names[i] = 0;
    }
  }
  return 0;
}

It contains much clutter to maintain a complete program and clean its resources, but the main point is to use strcpy to copy each token into its own string.

Eleni answered 12/11, 2011 at 19:8 Comment(0)
I
2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char** split(const char *str, const char *delimiter, size_t *len){
    char *text, *p, *first, **array;
    int c;
    char** ret;

    *len = 0;
    text=strdup(str);
    if(text==NULL) return NULL;
    for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item
        if(c==0) first=p; //first token top

    ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL
    if(ret==NULL){
        free(text);
        return NULL;
    }
    strcpy(text, str+(first-text));//skip until top token
    array=ret;

    for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){
        *array++=p;
    }
    *array=NULL;
    *len=c;
    return ret;
}

void free4split(char** sa){
    char **array=sa;

    if(sa!=NULL){
        free(array[0]);//for text
        free(sa);      //for array
    }
}

int main(void){
    char str[] ="test string.";
    char **words;
    size_t len=0;
    int i;

    words = split(str, " \t\r\n,.", &len);

/*
    for(char **wk = words; *wk ;wk++){
        printf("%s\n", *wk);
    }
*/
    for(i = 0;i<len;++i){
        printf("%s\n", words[i]);
    }
    free4split(words);
    return 0;
}
/* result:
test
string
*/
Intorsion answered 12/11, 2011 at 23:28 Comment(0)
B
1

Copy the results from strtok to a new buffer using a function such as

/*
 * Returns a copy of s in freshly allocated memory.
 * Exits the process if memory allocation fails.
 */
char *xstrdup(char const *s)
{
    char *p = malloc(strlen(s) + 1);
    if (p == NULL) {
        perror("memory allocation failed");
        exit(1);
    }
    strcpy(p, s);
    return p;
}

Don't forget to free the return values when you're done with them.

Bering answered 12/11, 2011 at 19:10 Comment(0)
U
1

IMO, you don't need (and probably don't want) to use strtok at all (as in, "for this, or much of anything else"). I think I'd use code something like this:

#include <string.h>
#include <stdlib.h>

static char *make_str(char const *begin, char const *end) { 
    size_t len = end-begin;
    char *ret = malloc(len+1);
    if (ret != NULL) {
        memcpy(ret, begin, len);
        ret[len]='\0';
    }
    return ret;
}

size_t tokenize(char *tokens[], size_t max, char const *input, char const *delims) { 
    int i;
    char const *start=input, *end=start;

    for (i=0; *start && i<max; i++) {
        for ( ;NULL!=strchr(delims, *start); ++start)
            ;
        for (end=start; *end && NULL==strchr(delims, *end); ++end)
            ;
        tokens[i] = make_str(start, end);
        start = end+1;
    }
    return i;
}

#ifdef TEST

#define MAX_TOKENS 10

int main() { 
    char *tokens[MAX_TOKENS];
    int i;
    size_t num = tokenize(tokens, MAX_TOKENS, "This is a longer input string ", " ");
    for (i=0; i<num; i++) {
        printf("|%s|\n", tokens[i]);
        free(tokens[i]);
    }
    return 0;
}

#endif
Under answered 12/11, 2011 at 20:0 Comment(0)
T
0

U can do something like this too.

    int main ()
    {
    char str[] ="test string.";

    char * temp1;
    char * temp2; 

    temp1 = strtok (str," ");

    temp2 = strchr(str, ' '); 
    if (temp2 != NULL)
        temp2++;

    printf ("Splitted string :%s, %s\n" , temp1 , temp2);
    return 
    }
Torytoryism answered 16/1, 2014 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.