![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Google for strtok. It doesn't create an array of tokens for you, it helps you looping through a string.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
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;
}
}
} |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2005
Posts: 12
Rep Power: 0
![]() |
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 = "" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|