Code:
/* consider the prototype */
char *strtok(char *one, const char *two);
This function is used to separate string one in our case into tokens separated by characters from the string two. The first call to strtok should include string one, and, to continue from the end of the previous token use NULL in the place of one in the subsequent calls. e.g
Code:
#include <stdio.h>
#include <string.h>
int main() {
char szSockData[20] = "msg file";
char *temp; /* temporary pointer to store the tokens before
transferring them*/
char szSock1[20];
char szSock2[20];
/*this is quite simple! :) */
temp = strtok(szSockData, " ");
memcpy(szSock1, temp, 20);
temp = strtok(NULL, " ");
memcpy(szSock2, temp, 20);
printf("\nszSock1: %s\nszSock2: %s\n", szSock1, szSock2);
}
This results in:
Code:
szSock1: msg
szSock2: file
Please note that you can use more than one character in string two (refer to the prototype above) e.g you could try this out
Code:
#include <stdio.h>
#include <string.h>
#define SEPARATING_CHARS "; ,."
/*tokens will be separated by any of the above characters */
int main()
{
char szSentence[]="Hey, how're you doing?, he asked. Fine";
char *temp;
temp = strtok(szSentence, SEPARATING_CHARS);
printf("\nThe words of this sentence are:\n%s\n", temp);
while((temp = strtok(NULL, SEPARATING_CHARS)) != NULL) {
printf("%s\n", temp);
}
}
The output of this small program is:
Code:
The words of this sentence are:
Hey
how're
you
doing?
he
asked
Fine
Hope this has been of help, let me know...
Cheers.