Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Coding my own string tokenizer?
Closed Thread
Old 11-20-2005, 02:53 AM   #1 (permalink)
beedubaya's Avatar
 
Lord Techie

Join Date: May 2004

Posts: 5,239

beedubaya is on a distinguished road

Default Coding my own string tokenizer?

My Advanced Programming teacher came up with a new rule that I can no longer use the provided "StringTokenizer" method, but have to write my own class file that does the same thing. I have no idea how I would go about doing that. Does anybody have anything that could help me out?
__________________
ASUS A8N-SLI Deluxe Motherboard
AMD Opteron 165 @ 2.25GHz
2GB G.Skill Extreme PC4000 RAM
eVGA 8800GT
Creative Sound Blaster X-Fi

"I know the human being and fish can co-exist peacefully" - George W. Bush
beedubaya is offline  
Old 11-20-2005, 04:59 AM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

What does the string tokenizer have to encapsulate?

The easiest thing is to separate it entirely from a specific input method (this way it is reusable) and therefore not responsible for its own file i/o methods.

You would ideally want to make a class (C++?, Java?) and pass it a string in the constructor. You would also want to pass a string containing string delimeter arguments....

for instance...

blah = new myStringTokenizer(myString, "\n\t ");

This would delimit strings by newline (\n), tab (\t) and whitespace( ).

Your class would ideally save the string to a member variable. Then it would save the delimiter string also.

You then want to call blah.nextToken() to return the next string token. Inside your class, you store the current location of your 'pointer', to remember where you left off, and incrementally build a string using a concatenation operation or another method until you encounter a delimeter character. Then you return said string.

[Another method if concatenation is not available (depending on what libraries you are using for string) is to store the start pointer, and move a working pointer along the string until you reach the delimeter char. Then you mark that position. You can then either call some variation of substring() or mid() or you can even then dynamically create the *correctly* sized string and loop back through from start pointer to end pointer copying char by char. This is much more efficient than concatenation]

Once delimeters are reached, you simply increment your position pointer along the string until a non-delimeter char is reached.

I would recommend having a member function/method called charisDelim which returns 1 if the given char matches a char in the delimeter string, or 0 otherwise.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 11-20-2005, 09:17 AM   #3 (permalink)
beedubaya's Avatar
 
Lord Techie

Join Date: May 2004

Posts: 5,239

beedubaya is on a distinguished road

Default

Thanks, TheHeadFL...that really helped me start thinking on the right path. I am still a bit confused, but I think I will get it worked out. I may be back with further questions.
__________________
ASUS A8N-SLI Deluxe Motherboard
AMD Opteron 165 @ 2.25GHz
2GB G.Skill Extreme PC4000 RAM
eVGA 8800GT
Creative Sound Blaster X-Fi

"I know the human being and fish can co-exist peacefully" - George W. Bush
beedubaya is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On