# Numeric Snippets and Functions

## Speed Distance Time Triangle

This triangle equation calculates the speed, distance or time of an entity.

Speed = Distance / Time
Distance = Time / Speed
Time = Distance / Speed

# String-based Snippets and Functions Note The string-based functions that are listed here (strtok and split) have a far better alternative available: Sscanf. Use of these functions is generally discouraged.

## Strtok

Extracts a substring where a character or token is found.

### Parameters

• string: the string to split up.
• index: a numeric position in where to search the string to start from.
• seperator: the character or token to search for.

### Source

```//Faster strtok Code by Jeffry
stock strtok(const string[], &index)
{
new result, length = strlen(string), i = index;
while ((i < length) && (string[i] == ' ')) i++;
strmid(result,string,i,((index = strfind(string, " ", false, i)) == -1) ? (index = length) : (index) , 20);
index++;
return result;
}

// Written by DracoBlue.
stock strtok(const string[], &index,seperator=' ')
{
new length = strlen(string);
new offset = index;
new result[MAX_STRING];
while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}

result[index - offset] = EOS;
if ((index < length) && (string[index] == seperator))
{
index++;
}
return result;
}```

### Example

Here we will do a shouting command.

```if(strcmp(cmdtext,"/shout",true)==0)
{
new
tmp, //The variable for strtok.
string, // A Variable where we store a text/number with information.
pName[MAX_PLAYER_NAME], //You can use this variable to get the players name later on.
idx; //Also known as Index,Part of strtok.

tmp = strtok(cmdtext,idx); //This Is where we define tmp to equal strtok.

if(!strlen(tmp))
return SendClientMessage(playerid,COLOR_WHITE,"USAGE:/me[action]");
//this will see if the playerid filled all parameters,if not send him a message showing him how.
//now continue with the command.

GetPlayerName(playerid,pName,sizeof(pName));//This Stores the playerid's name in a variable.
format(string,sizeof(string),"%s Shouts: %s !!",pName,tmp);
/* In the format up,We used the stored text in the pName variable to show the player's name.
Then we used the tmp variable to get the text that the player entered.*/
SendClientMessageToAll(COLOR_WHITE,string); //now we send the formatted string.
return 1;//make sure it returns something.
}//and close the ending bracket

/* Written by iMatt */
/* Optimized by Vince */```

## Split

Works similar to strtok, except it splits up the entire string.

### Parameters

• strsrc: String to split up.
• strdest: A 2D array; where to store the split up pieces.
• delimiter: A character or token which identifies the beginning and end of substrings.

### Source

```//Faster (new) split Code [[User:Kaliber|Kaliber]]
stock split(const src[], dest[][], const delimiter)
{
new n_pos,num,old,str;
str = delimiter;
while(n_pos != -1)
{
n_pos = strfind(src,str,false,n_pos+1);
strmid(dest[num++], src, (!num)?0:old+1,(n_pos==-1)?strlen(src):n_pos,256);
old=n_pos;
}
return 1;
}

// Author unknown. It was probably someone smart like [[User:DracoBlue|DracoBlue]] or [[User:Y_Less|Y_Less]].
stock split(const strsrc[], strdest[][], delimiter)
{
new i, li;
new aNum;
new len;
while(i <= strlen(strsrc))
{
if(strsrc[i] == delimiter || i == strlen(strsrc))
{
len = strmid(strdest[aNum], strsrc, li, i, 128);
strdest[aNum][len] = 0;
li = i+1;
aNum++;
}
i++;
}
return 1;
}```

### Example

```public OnFilterScriptInit()
{
new tmp;
split("Hello World!", tmp, ' ');
print(tmp);
}```
```OUTPUTS: Hello
```