Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Tokenizer Problem

Status
Not open for further replies.

CsuperC

Programmer
Oct 7, 2003
1
PL
I am creating my own string tokenizer - yet I am having problem with my nextToken method - the method should return the next token and move the counter to the start of the next token. I am having issues with appended a character on to the end of a temp string - here is my method -

char* nextToken(stringTokenizer *strTok)
{

char *tempString;
char delim;
int index;

tempString = (char *)malloc(20*sizeof(char));

delim=(*strTok)->delimiter;
index=(*strTok)->currIndex;

while(((*strTok)->charLine[index])!= delim)
{
//append (*strTok)->charLine[index] to
//tempString ?????
index++;
}

(*strTok)->currIndex = index+1;

return tempString;
}

how can I append a char each time to my temp string???

:)

cheers!
 
I think this is what you want:-

unsigned int count = 0;

while(((*strTok)->charLine[index])!= delim)
{
if(count <= 18) // 18 as 19th slot for '\0'
{
// Append a character
tempString[count++] = (*strTok)->charLine[index];
}
else
break;

index++;
}

tempString[count] = '\0';

Hope this helps
But if you think a bit more about this, it can be improved a lot in terms of performance and memory usage
 
If you're creating your own tokenizer as a learning experience or as a school assignment (the two are usually mutually exclusive), that's fine.

If not, however, you might consider going to and getting their perfectly good tokenizer library.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top