0

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?

2
  • 1
    Possible duplicate of stackoverflow.com/questions/12582955/… Commented Oct 2, 2012 at 22:44
  • not really, I am not looking to remove white spaces, I am more concerned about obtaining the words Commented Oct 3, 2012 at 15:48

1 Answer 1

4
/* 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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you @andro1d this a good start. Am I able to tell strtok to ignore \n and \t? Eg.: - This, a sample \t string. to produce outcome: - This, a sample string
As it is right now it places the \t alone in a line. thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.