My Tech notes: strtok Simple Usage Example string words seperate
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Thursday, October 30, 2008

strtok Simple Usage Example string words seperate

How to get string words separate? Separate each word from string of /share/test/
Here is simple code snippet which also demonstrate use of string token (strtok() function);
Below is simple C code;
#include 
#include

int main()
{
char df_buf[120];
char *name1,*result;
int i;
char *tmp;
char str_array[7][50];

strcpy(df_buf,"/share/test/path/");
result=strtok(df_buf,"/");

//printf("%s\n",result);
while(result!=NULL)
{
tmp=strdup(result);
strcpy(str_array[i],tmp);
result=strtok(NULL,"/");
printf("%s and %s\n",result,tmp);
i++;
}
printf("i=%d\n",i);
printf("%s\n",str_array[0]);
printf("%s\n",str_array[1]);
printf("%s\n",str_array[2]);
return 0;
}
The output will be
share
test
path
ie /share/test/path string is separated into words;

No comments:

Post a Comment