Tokenize String : an alternate to strtok

10/08/2009 ·

This function tokenizes a given string based on white spaces(Blank spaces,new line & tabs) . If you want to tokenize on a particular character like ',' or something else pass the character as c_Delimiter in the function argument. Following test program describes the use of the function.


#include "stdio.h"
#include "string.h"
int TokenizeString(char *s_String, char s_Token[][25], char c_Delimiter)
{
int j = 0;
unsigned int i_Offset = 0;
char b_Flag = 0;
int count = 0;
for (i_Offset = 0;i_Offset <= strlen(s_String);i_Offset++)
{
if (s_String[i_Offset] != c_Delimiter && s_String[i_Offset] != '\t' && s_String[i_Offset] != '\n' && s_String[i_Offset] != '\0')
{
s_Token[count][j] = s_String[i_Offset];
j++;
b_Flag = 1;
continue;
}
if (b_Flag)
{
s_Token[count][j] = '\0';
count++;
j = 0;
b_Flag = 0;
}
}
return (count - 1);
}
int main()
{
char s_SrcString[200]="here we go with what we can do,cant do";
char s_Token[15][25];
memset(s_Token,0,200);
int count=TokenizeString(s_SrcString,s_Token,' ');
int i;
for(i=0;i<=count;i++)
{
printf("%s\n",s_Token[i]);
}
return 0;
}

Delimiter : White Spaces
Tested and compiled with: gcc and g++
Input: here we go with what we can do,cant do
Output :
here
we
go
with
what
we
can
do,cant
do

3 comments:

Abhishek said...
27/10/2009, 18:41  

Thanks for the code, i was searching for it very long

Unknown said...
21/10/2014, 01:24  

this doesn't work in c in linux

SHARAD KUMAR SINGH said...
21/10/2014, 01:44  

limpho - are you getting any errors. It would be great if you can send that and try compiling with g++

Post a Comment

Submit Request

If you need some code or programs just post a comment on this post. I will try to provide you the same at the earliest.

About this blog

Free sample code, example code , code snippet , tutorials in C C++. Find, download and reuse the code database available which vary from small programs to large ones as well. Feel free to request for code that is not in the list.

Followers