简体   繁体   中英

Place words of a string into an array of strings - c programming

I am trying to get a function to split a string containing several words which are separated by 1 or more spaces and put each word without any spaces into an index of an array of strings.

I have been googling it for a while, it seems I need strtok but I am a bit clueless, would someone please shed some light?

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ");
  }
  return 0;
}

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