[Tutorial] strtok
#1

I wrote this tutorial for a friend of mine who's learning but can't find any tutorial about strtok, and since there seems to be a lack of such threads, I'll post mine here. I hope it helps someone, even though strtok is useless compared to sscanf.

Here is an example of a basic command using three parameters:
pawn Code:
CMD:order(playerid, params[])
{
    // First we create the three strings and idx
    new
        string1[30],
        string2[30],
        string3[30],
        idx; // idx helps the function strtok to remember which word it last took from a string
       
    // Let's say the player types "/order extra crispy fries"
    // /order is used in the yCMD/zcmd/whatever system you're using and the rest of the string ("extra crispy fries") is stored in params, the string
    // We use strtok to grab the first word:
    format(string1, 30, strtok(params, idx));
    // PLEASE NOTE that strtok doesn't CHANGE the string, params is still the same as before!
    // We simply store the location of the space, like a bookmark in a book, for the function strtok
   
    // Now string1 contains "extra".
    // idx is changed from 0 (Which it is when we create it) to a representational symbol of the location of the space
    // in the string for the strtok function so it knows where it should start now that we do it again.
   
    // Right now, strtok will start at the position marked with an *, which is a space: "extra*crispy fries".
    format(string2, 30, strtok(params, idx));
    // It got the word "crispy" from the string, since it's all the letters from the * to the next space!
    // And now, we get "fries" since idx represents this position: "extra crispy*fries":
    format(string3, 30, strtok(params, idx));
    // So now we have three strings:
    // string1: "extra"
    // string2: "crispy"
    // string3: "fries"
    // And also:
    // params: "extra crispy fries"
   
    // Since the function we use to compare strings - strcmp - returns the same value if a string is empty as if
    // it was what we're looking for, we need to make sure that the variables contain something. Let's put the check
    // inside the string comparison part! (We use the function "isnull")
   
    // Now we can start checking if the player wrote extra, then crispy, then fries!
    if(!strcmp(string1, "extra", false, 5) && !isnull(string1)) // Always put in the maximum size of the word if you know what it is (5)
    {
        if(!strcmp(string2, "crispy", false, 6) && !isnull(string2))
        {
            if(!strcmp(string3, "fries", false, 5) && !isnull(string3))
            {
                SendClientMessage(playerid, 0xFF0000AA, "You've ordered some extra crispy fries!");
                return 1;
            }
            SendClientMessage(playerid, 0xFF0000AA, "You forgot fries!");
            return 1;
        }
        SendClientMessage(playerid, 0xFF0000AA, "You forgot crispy fries!");
        return 1;
    }
    SendClientMessage(playerid, 0xFF0000AA, "You forgot extra crispy fries!");
    return 1;
}
Reply


Messages In This Thread
strtok - by Lenny the Cup - 08.11.2010, 12:54
Re: strtok - by Lenny the Cup - 08.12.2010, 22:26
Re: strtok - by Scenario - 08.12.2010, 23:13
Re: strtok - by __ - 10.12.2010, 10:11
Re: strtok - by Lenny the Cup - 12.12.2010, 18:11
Re: strtok - by [NWA]Hannes - 12.12.2010, 18:29
Re: strtok - by Lenny the Cup - 13.12.2010, 10:05

Forum Jump:


Users browsing this thread: 1 Guest(s)