Function:strtok

From SA-MP Wiki

Jump to: navigation, search

This function searching space in line.

strtok(const string[], &index) 
{ 
	new length = strlen(string); 
	while ((index < length) && (string[index] <= ' ')) index++; 
	new offset = index, result[20]; 
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1))) 
	{ 
		result[index - offset] = string[index]; 
		index++; 
	} 
	result[index - offset] = EOS; 
	return result; 
}

by <__Ǝthan__>

Edited function for searching any symbols in line, not only space.

stock strtok(const string[],seperator[2],&index) 
{ 
	new length = strlen(string); 
	while ((index < length) && (string[index] <= seperator[0])) index++; 
	new offset = index, result[20]; 
	while ((index < length) && (string[index] > seperator[0]) && ((index - offset) < (sizeof(result) - 1))) 
	{
		result[index - offset] = string[index]; 
		index++; 
	} 
	result[index - offset] = EOS; 
	return result; 
}

by 009

strtok.inc

/* extract words from a string (words are separated by white space) */ 
#include <string> 
strtok(const string[], &index) 
{ 
	new length = strlen(string);
/* skip leading white space */ 
	while (index < length && string[index] <= ’ ’) index++; 
/* store the word letter for letter */ 
	new offset = index; /* save start position of token */ 
	new result[20]; /* string to store the word in */ 
	while (index < length 
	&& string[index] > ’ ’ 
	&& index - offset < sizeof result - 1) 
	{ 
		result[index - offset] = string[index];
		index++;
	} 
	result[index - offset] = EOS; /* zero-terminate the string */ 
	return result;
}

by Remis93

Personal tools
Navigation
Toolbox