Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 29th, 2005, 6:17 AM   #1
HackeZ
Programmer
 
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0 HackeZ is on a distinguished road
Splitting Char * into Tokens.

Hey I was just fooling around off work, and I was having some problems splitting some char's i declared into tokens, I tried memset(); with no prevail. Any idea how I might fix this silly mistake. No big deal if you cant help just foolen around and learning.

Btw by tokens incase some are wondering im referring to say a string like "This is a string" split into Token[0] = This, Token[1] = is, Token[2] = a, ... etc.
HackeZ is offline   Reply With Quote
Old Dec 29th, 2005, 6:24 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Google for strtok. It doesn't create an array of tokens for you, it helps you looping through a string.
Polyphemus_ is offline   Reply With Quote
Old Dec 29th, 2005, 11:11 AM   #3
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
http://www.programmingforums.org/for...1&postcount=11

Scan through your string replacing the delimiter (a space in your case) with a NULL. Save the address to the beginning of each of these in an array.
para is offline   Reply With Quote
Old Dec 29th, 2005, 12:14 PM   #4
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
you could try my method,not the best way, but it works

void split(string &input, vector<string> &str, char delim[])
{
     while(input.length() > 0)
     {
        if(input.find(delim,0) != string::npos)
        {
            str.push_back(input.substr(0,input.find(delim,0)));
            input.erase(0,input.find(delim,0)+1);
            
        }
        else
        {
            str.push_back(input);
            break;
        }
     }
}
Master is offline   Reply With Quote
Old Dec 30th, 2005, 4:31 PM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Master your function is not bad at all. And nice C++ instead of strtok or raw pointers from C.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 30th, 2005, 6:57 PM   #6
Jerkstore
Newbie
 
Join Date: Dec 2005
Posts: 12
Rep Power: 0 Jerkstore is on a distinguished road
I like that Master
I would have just done a for loop from 0 to input length and sub-stringed each character to a tempString...if char is a space then push_back on vector and reset the tempString = ""
Jerkstore is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC