aboutsummaryrefslogtreecommitdiff
path: root/tests/strsep.c
blob: aa8e1ad71fc2ae6f2c1888f09006e6a8105a545e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, char **argv)
{
  int i = 0;
  char *tok, *p_tok;
  char **arr;

  if (argc != 4) {
    fprintf(stderr, "usage: %s [ARR_SIZ] [DELIM] [STRING]\n", argv[0]);
    exit(1);
  }

  arr = calloc(atoi(argv[1]), sizeof(char *));
  p_tok = argv[3];
  while ( (tok = strsep(&p_tok, argv[2])) != NULL ) {
    arr[i] = tok;
    i++;
  }

  i = 0;
  while ( arr[i] != NULL ) {
    printf("ARRAY[%d]: %s\n", i, arr[i]);
    i++;
  }
  return 0;
}