简体   繁体   中英

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(). 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()

UPDATE

The OP needs to clarify what exactly he wants to get from this splitString function?

It returns an array of pointers to separated words. That's fine if he used strtok , as it would terminate each word with 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. 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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