C String Tokenizer

In this section, you will learn how to use strtok()
function to break the
string into a series of tokens. You can see in the given example, we have define
a string and a pointer. The expression
ch = strtok (st," ") extract the string from the string sequence
one by one and printf ("%s\n", ch) prints the string after being
tokenized. The expression
ch = strtok (NULL, " ,") extract the whole string from the string
sequence till the ch get equal to null.
Here is the code:
TOKENIZE.C
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
char st[] ="Where there is will, there is a way.";
char *ch;
clrscr();
printf("Split \"%s\"\n", st);
ch = strtok(st, " ");
while (ch != NULL) {
printf("%s\n", ch);
ch = strtok(NULL, " ,");
}
getch();
return 0;
}
|
Output will be displayed as:
TOKENIZE.EXE

Download Source Code:

|