Introduction
Thanks for your question Ahmet. String tokenizer is one of the most strange functions of C string library. First let me talk about tokenization concept. Assume you are given a string and another fixed delimiter string. Say your string is “The great grandson of Husnu Sensoy” and the delimiter is happy space character ” “. A token is defined to be any minimum charactered string of the given string between either two delimeter (” “) string or between a delimeter string and string start/end. So the tokens for our example are:
The
great
grandson
of
Husnu
Sensoy
Next question is that how to do this in C ? Here is it:
#include <stdio.h>
#include <string.h> /*Required for strtok function*/
int main() {
char string[] = “The greatgrandson of Husnu Sensoy”;
char* token;
const char dlm[] = ” “;
token = strtok(string,dlm);
while( token != NULL) {
printf(“%s\n”, token);
token = strtok(NULL,” “);
}
return 0;
}