简体   繁体   English

在 C 中不使用 strtok() 拆分字符串

[英]Split string without using strtok() in C

I am trying to split a sentence into an array of the individual words.我正在尝试将一个句子拆分为一组单个单词。

I have recently realized that I cannot use strtok() in this function and am now looking for an alternative way to implement this function without using strtok().我最近意识到我不能在这个函数中使用 strtok(),现在正在寻找一种替代方法来实现这个函数而不使用 strtok()。 Does anyone know how I can go about this?有谁知道我该怎么做?

Edit:编辑:

I need to return a pointer to an array of pointer pointing to the individual words in the sentence.我需要返回一个指向指向句子中各个单词的指针数组的指针。

Use strchr() instead of strtok()使用strchr()而不是strtok()

UPDATE更新

The OP needs to clarify what exactly he wants to get from this splitString function? OP 需要澄清他到底想从这个splitString函数中得到什么?

It returns an array of pointers to separated words.它返回一个指向分隔单词的指针数组。 That's fine if he used strtok , as it would terminate each word with 0 .如果他使用strtok ,那很好,因为它会用0终止每个单词。

If those 0 's are not inserted, the resulting array, while still pointing to the beginning of each word, would have no indication of their lengths.如果未插入那些0 ,则结果数组虽然仍指向每个单词的开头,但不会指示其长度。 That should be handled by the user of this array.这应该由该数组的用户处理。

Copied from my answer to a now closed question at https://stackoverflow.com/a/63866151/13422复制自我对https://stackoverflow.com/a/63866151/13422 上现已结束的问题的回答

You would look through the list of functions provided by the C <string.h> header file in the C Standard Library and you'd find a lot of options.您可以查看 C 标准库中 C <string.h>头文件提供的函数列表,您会发现很多选项。

I wrote something just for fun:我写了一些只是为了好玩:

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

struct token {
  const char *str;
  size_t len;
};

struct token next_token(const char *str, const char *sep) {
  struct token tok;
  /* skip leading separator characters */
  str += strspn(str, sep);
  tok.str = str;
  tok.len = strcspn(str, sep);
  return tok;
}

struct token *get_tokens(const char *str, const char *sep) {
  size_t len = 0, cap = 8;
  struct token *arr = malloc(cap * sizeof *arr);

  for (struct token tok = next_token(str, sep); tok.len;
       tok = next_token(tok.str + tok.len, sep)) {
    arr[len] = tok;
    ++len;
    if (len == cap) {
      cap *= 2;
      arr = realloc(arr, cap * sizeof *arr);
    }
  }
  arr[len].str = NULL;
  arr[len].len = 0;
  return arr;
}

int main(int argc, char *argv[]) {
  if (argc < 2)
    exit(EXIT_FAILURE);

  puts("Token array");
  struct token *token_arr = get_tokens(argv[1], " \t\n");
  for (size_t i = 0; token_arr[i].str; ++i) {
    printf("\"%.*s\" ", (int)token_arr[i].len, token_arr[i].str);
  }
  putchar('\n');
  free(token_arr);

  puts("Next token loop");
  for (struct token tok = next_token(argv[1], " \t\n"); tok.len;
       tok = next_token(tok.str + tok.len, " \t\n")) {
    printf("\"%.*s\" ", (int)tok.len, tok.str);
  }
  putchar('\n');
  return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM