[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
#2

Doesn't seem like anyone needs it? :P I'm sure it's useful to someone?
Reply
#3

Why don't you just tell your friend to use sscanf? It's probably a lot easier to learn the strtok.
Reply
#4

You shouldn't be convincing people to learn about redundant and inefficient methods, sscanf beats strtok any day as you probably know.
Reply
#5

Quote:
Originally Posted by Lenny
I hope it helps someone, even though strtok is useless compared to sscanf.
Not convincing anyone, it's useful to learn all methods.

I even benchmarked them to prove it.
Reply
#6

https://sampwiki.blast.hk/wiki/strtok
I dont know with you, but i would say that is a tutorial.
Reply
#7

Damn, I couldn't find that :P Well at least mine is shorter
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)