使用C中的strtok [英] Using strtok in c

查看:95
本文介绍了使用C中的strtok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用的strtok在第一和最后一个名称阅读和独立的。我怎么可以存储在那里我可以在两个不同的字符数组idependently使用它们的名字呢?

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
  烧焦海峡[] =测试字符串。
  字符*测试;
  测试= strtok的(STR,);
  而(测试!= NULL)
  {
    的printf(%S \\ n,测试);
    测试= strtok的(NULL,);
  }
  返回0;
}


解决方案

下面是我在拿一个相当简单的记号化帮手


  • 结果存储在一个动态增长的数组

  • 空终止阵列

  • 保持输入字符串安全(strtok的修改输入字符串,它是的未定义行为的对文字的cha​​r [],至少我觉得在C99)

为了使code重入,使用非标准的 strtok_r

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;焦炭**记号化(为const char *输入)
{
    字符*海峡=的strdup(输入);
    诠释计数= 0;
    INT容量= 10;
    焦炭**结果=的malloc(容量* sizeof的(*结果));    字符* TOK = strtok的(STR,);    而(1)
    {
        如果(计数> =容量)
            结果= realloc的(结果,(容量* = 2)* sizeof的(*结果));        结果[统计++] = TOK?的strdup(TOK):TOK;        如果中断(TOK!);        TOK =的strtok(NULL,);
    }    免费(STR);
    返回结果;
}诠释的main()
{
    焦炭**令牌=记号化(测试字符串。);    焦炭**它;
    对于(IT =令牌,它和放大器;&放大器; *它;它++)
    {
        的printf(%S \\ n,*吧);
        免费(*它);
    }    免费的(标记);
    返回0;
}

这是一个 strtok的 - 免费的重新实现的(使用 strpbrk 代替)

 的char **记号化(为const char *海峡)
{
    诠释计数= 0;
    INT容量= 10;
    焦炭**结果=的malloc(容量* sizeof的(*结果));    为const char * E = str中;    如果(E)做
    {
        为const char * S = E;
        E = strpbrk(S,);        如果(计数> =容量)
            结果= realloc的(结果,(容量* = 2)* sizeof的(*结果));        结果[统计++] = E? strndup(S,E-S)的strdup(S);
    }而(E&安培;&放大器; *(++ e)条);    如果(计数> =容量)
        结果= realloc的(结果,(容量+ = 1)* sizeof的(*结果));
    结果[统计++] = 0;    返回结果;
}

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

解决方案

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

这篇关于使用C中的strtok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆