strtok() is one option. You can look it up in your fav C reference yourself, but here's an example:
Code:
#include <stdio.h>
#include <string.h>
int main() {
int i=1, n;
char string[]="The quick fox jumps over the lazy brown dog.";
char ray[16][32], *tok;
tok=strtok(string," "); /* first instance */
strcpy(ray[0],tok);
while ((tok=strtok(NULL," "))!=NULL) {
strcpy(ray[i],tok);
i++;
}
/* now verify */
for (n=0; n<i; n++) printf("%s\n",ray[n]);
return 0;
}
Two points to remember:
- the first time pass a pointer to strtok, after that pass it NULL and it will progress thru the string originally indicated.
- strtok will destroy the original string fed to it in the process
[later] Maz's idea below is more elegant and optimizable but s/he's wrong about strtok, it's great for parsing input, etc, where the string is a local stack variable that's getting discarded anyway, so worth learning how to use properly (or don't bother and just consider it "awful"). Also, Maz's solution would be a waste of time on such a local variable; you'd have to make copies anyway. Apples, oranges, tomato...