Implement strtok() function. (It tokenizes the string )

Implementation of strtok

char* strtok(char *s, const char *delim) {
static char *old = NULL;
char *token;
if(! s) {
s = old;
if(! s) {
return NULL;
}
}
if(s) {
s += strspn(s, delim);
if(*s == 0) {
old = NULL;
return NULL;
}
}

token = s;
s = strpbrk(s, delim);
if(s == NULL) {
old = NULL;
} else {
*s = 0;
old = s + 1;
}
return token;
}

Related Post

Write a function to reverse the words in a string? (e.g. “Your time has come” – > “come has time Your”)
String Number conversion in JavaScript
String in Java?
What will be output of the following program?
String.intern in Java

Comments

Leave a Reply