应用bitmap实现的strtok

// Basic.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;


char *strtok(char *str, const char *delim)
{
	static char *last;
	if (delim == NULL || (str == NULL && last == NULL))
		return NULL;

	if (str == NULL)
	{
		str = last;	
	}
	
	char arr[32];
	int i=0;
	for(i=0; i < 32; i++)
	{
		arr[i] = '\0';
	}
	const char *dpos=delim;
	while(*dpos)
	{
		arr[*dpos >> 3] |= (1 << (*dpos & 7));
		dpos++;
	}
	
	while(*str && (arr[*str >> 3] & (1 << (*str & 7))) )
		str++;

	char *tar = str;
	
	while(*str && !(arr[*str >> 3] & (1 << (*str & 7))))
		str++;

	if(*str != '\0')
	{
		*str = '\0';
		last = str+1;
	}
	else{
		last = NULL;
	}
	
	return tar;
}

void test_strtok()
{
	printf( "Tokens:\n" );

	// Establish string and get the first token:
	token = strtok( string, seps ); // C4996
	// Note: strtok is deprecated; consider using strtok_s instead
	while( token != NULL )
	{
		// While there are tokens in "string"
		printf( "%s\n", token );

		// Get next token: 
		token = strtok( NULL, seps ); // C4996
	}
}

 

int _tmain(int argc, _TCHAR* argv[])
{
	test_strtok();
}


你可能感兴趣的:(c)