- strtok( ) function in C tokenizes/parses the given string using delimiter. Syntax for strtok( ) function is given below.
char * strtok ( char * str, const char * delimiters );
Example program for strtok() function in C:
- In this program, input string “Test,string1,Test,string2:Test:string3″ is parsed using strtok() function. Delimiter comma (,) is used to separate each sub strings from input string.
#include <stdio.h>
#include <string.h>
int main ()
{
char string[50] ="Test,string1,Test,string2:Test:string3";
char *p;
printf ("String \"%s\" is split into tokens:\n",string);
p = strtok (string,",:");
while (p!= NULL)
{
printf ("%s\n",p);
p = strtok (NULL, ",:");
}
return 0;
}
Output:
|
String “Test,string1,Test,string2:Test:string3″ is split into tokens:
Test
string1
Test
string2
Test
string3
|






0 comments:
Post a Comment