Topic Name: strtok
| | strtok pls tell me abt colored part wat does ",.-" stands for...... #include <stdio.h> #include <string.h>
int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0; }
Output:
Splitting string "- This, a sample string." into tokens: This a sample string
| 2nd Nov 09 05:53:09 AM |
| | RE: strtok strtok scans the first character array for the first token not contained in the second array.
The function will return the address in the first array if any of the character in the second string is found.
In the subsequent calls u don't need to pass the first string again and again, you can simply pass NULL then it will search the same string from the next postions.
#include <stdio.h> #include <string.h>
int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); //returns address where the first token is found pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); //searches from the next postion in the string for the next token pch = strtok (NULL, " ,.-"); // make sure to pass NULL otherwise it will search from the begining again } return 0; }
Hope that this will help you to clear ur doubts ...
regards Joydeep
| 5th Nov 09 02:06:30 PM |
| | |