There are many ways to split a line of text up into tokens. One simple example is to split up the words in a sentence. The following code shows how to do this using the
strtok()
function. Just beware that this function alters the original string, so if you need to use it again, make a copy before calling strtok()
.
#include
#include
#define DELIM " "
#define MAXWORD 80
#define MAXLEN 20
int main(void)
{
char words[MAXWORD][MAXLEN];
char buff[BUFSIZ];
int ntokens = 0;
int i;
printf("Enter a string: ");
fflush(stdout);
if (fgets(buff, sizeof buff, stdin) != NULL)
{
char *sep = strtok(buff, DELIM);
while (sep != NULL)
{
strcpy(words[ntokens++], sep);
sep = strtok(NULL, DELIM);
}
}
for (i = 0; i < ntokens; i++)
{
puts(words[i]);
}
return(0);
}
/** Output : Enter a string: this is a long line of text
this
is
a
long
line
of
text **/