在c中使用strtok

时间:2011-11-12 18:57:13

标签: c strtok

我需要使用strtok来读取名字和姓氏并将其分开。如何将名称存储在两个独立的char数组中?我可以将它们存储在哪里?

#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;
}

7 个答案:

答案 0 :(得分:19)

这是我对一个相当简单的标记化帮助

的看法
  • 将结果存储在动态增长的数组中
  • null-terminated the array
  • 保持输入字符串安全(strtok修改输入字符串,这是文字char []上的未定义的行为,至少我认为在C99中)

要使代码可重入,请使用非标准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;
}

以下是 strtok - 免费重新实现(使用strpbrk代替):

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;
}

答案 1 :(得分:7)

您需要单独存放吗?两个指向修改后的char数组的指针将产生两个独立的完全可用的字符串。

那就是我们改变这个:

char str[] ="test string.";

进入这个:

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;
}

答案 2 :(得分:4)

如何使用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;
}

它包含了维护完整程序和清理其资源的混乱,但重点是使用strcpy将每个标记复制到自己的字符串中。

答案 3 :(得分: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
*/

答案 4 :(得分:1)

使用

等功能将结果从strtok复制到新缓冲区
/*
 * 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;
}

完成后,不要忘记free返回值。

答案 5 :(得分:1)

IMO,您根本不需要(也可能不想)使用strtok(例如,“为此,或其他任何东西”)。我想我会使用这样的代码:

#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

答案 6 :(得分:0)

你也可以这样做。

    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 
    }