Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Help with strin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 4250 of 4370
Post > Topic >>

Help with string tokenization

by kevinday.home@[EMAIL PROTECTED] Aug 27, 2008 at 10:17 PM

I have a program I'm working on for a class.  We have to break a file
up into all of the "words" in a file, and then insert each word into
an ordered list.  If the word is already in the list we increment the
count of the node rather than inserting a duplicate.  When it's
finished we output the list of all the "words" in the file and the
number of times each word occurred.  I've got most of it complete, but
have one problem...
we're working with gnu's gcc compiler and we're not allowed to use STL
or the string class(we essentially have to create our own) in
microsoft c# I know there is a string.split(delims) method, I'm
basically trying to duplicate it in my string class and here's what I
have....

myString *myString::split(int &tokenCount,const myString &delim)
{
	myString *tokens=NULL;
	char *temp;
	char *saveData=new char[length+1];
	strcpy(saveData,data);
	tokenCount=0;
	temp=strtok(saveData,delim.data);
	while(NULL!=temp)
	{
		tokenCount++;
		temp=strtok(NULL,delim.data);
	}
	if(tokenCount>0)
	{
		tokens=new myString[tokenCount];
		strcpy(saveData,data);
		tokens[0]=strtok(saveData,delim.data);
		for(int i=1;i<tokenCount;i++)
		{
			tokens[i]=strtok(NULL,delim.data);
		}
	}
	return tokens;
}


The above code seems to work for all my tests, but I'm worried with
the dynamic memory allocation of the return that I'm leaking memory.
Which of course is bad practice, and will get me an automatic zero on
the assignment.  I'm looking for a solution where I can return the
array of tokens, rather than pass the token array in by reference.  Is
that impossible without leaking memory in the process?  and without
using global scope variables?
 




 4 Posts in Topic:
Help with string tokenization
kevinday.home@[EMAIL PROT  2008-08-27 22:17:38 
Re: Help with string tokenization
Ben Bacarisse <ben.use  2008-08-28 13:42:56 
Re: Help with string tokenization
Bart van Ingen Schenau &l  2008-08-28 18:51:23 
Re: Help with string tokenization
kevinday.home@[EMAIL PROT  2008-08-29 03:33:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 15:10:58 CST 2008.