C - Write program to Analyze comma-separated list of words

Requirements

Extract the words from a sentence and output them one to a line, removing any leading or trailing spaces.

For example, if the input is: test , a , test

then the output will be:

test
a
test

Demo

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_LEN  5000                                      // Maximum string length
int main(void)
{
  char list[MAX_LEN] = "this  ,  is,  a,,  test";   // Stores the list of comma separated words
  const char comma[] = ",";                                // The only word delimiter

                               // Remove spaces
  size_t index = 0;                                        // Character position
  size_t i = 0;//from ww w  .  ja v a2s.c  o m
  do
  {
    if (isspace(list[i]))                            // If it's whitespace...
      continue;                                     // ... skip the character...
    list[index++] = list[i];                        // ... otherwise copy the character
  } while (list[i++] != '\0');

  // Find words in list
  char *ptr = NULL;
  size_t list_len = strnlen_s(list, MAX_LEN);
  char *pWord = strtok_s(list, comma, &ptr);    // Find 1st word
  if (pWord)
  {
    do
    {
      printf("%s\n", pWord);
      pWord = strtok_s(NULL, comma, &ptr);      // Find subsequent words
    } while (pWord);                                         // NULL ends tokenizing
  }

  return 0;
}

Result