Splitting A String Into Tokens!
Right, I am having a little trouble with making a string tokeniser to split up a string!
NOTE: This is for university coursework. So please just give examples; not a complete solution!
and a typical request looks something like:
BREW POT-1 HTCPCP/1.0\r\nAccept-Additions:#milk-type;skimmed#milk-amount;lots\r\n
obviously there is a little bit missing in the above request (syrup, sweetener etc.)
All i am trying to do is to split up the string into tokens so i can process the request! at the server end!!!!!
i want to be able to have the tokens;
BREW
POT-1
HTCPCP/1.0
....
using delimeters " ;#\r\n"
How can i do this??? ...All i want to do at the moment is print the tokens to the screen as the request arrives!!!
Here is the code that I have done so far, but its not working!!!!!!!!
Code:
char Array[MAX_ARRAY] = "BREW POT-1 HTCPCP/1.0\r\nAccept-Additions:#milk-type;skimmed#milk-amount;lots\r\n";
char *TmpArray;
char *Tokens[MAX_TOKEN];
int TokenCount;
char *ptr;
TokenCount = 0;
TmpArray = strdup(Array);
for (ptr = TmpArray; *ptr;) {
while (*ptr == ' ') /* ignore leading space AND treat consecutive spaces as a single separator */
*(ptr++) = 0; /* replace the space with end-of-string */
if (++TokenCount >= MAX_TOKEN) /* check for too many tokens. Ignore the extras. */
Tokens[TokenCount] = ptr; /* Save the address of the current token */
while (*ptr && *ptr != ' ') /* Skip the contents of the token */
ptr++;
}
for (i = 0; i < TokenCount; i++){
printf("tokens: %s\n", Tokens[i]);
}
Thanks for your help!!!! Matt.