Sign in to follow this  
Odin1985

a question about Char and Strings

Recommended Posts

im wondering if its posible, in c++, that having a group of words, by storing them as a char array or a string, to get each one by separate and have a operator look at them, for example having the words " hello cute world " to have stored atleast in a temp variable, each word by separete, like array[1]=hello array[2]=cute array[3]=world and then be able to work then them like having an if check for like (array[] == cute) or something like that? i know that with the strings and things liek assing, and subtr you can have part of the string given back, but can i have it each word only? like having it chopped where each space is?

Share this post


Link to post
Share on other sites
He's looking for a split function in C++. I can't remember if there's one off hand or not. I suspect not, which will require a 3rd party lib or manual manipulation.

There's probably a half dozen semi-immediate ways to do it, none of them particularly good. C++ is notoriously horrible at string manipulation.

Share this post


Link to post
Share on other sites
Quote:
Original post by EdR
I'm trying to figure out what you're doing, but I'm drawing a blank.

Tokenization?

Google gives me this:

#include <string>
#include <vector>
using namespace std;

void tokenize(const string& str,const string& delimiters, vector<string>& tokens )
{
// skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);

// find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);

while (string::npos != pos || string::npos != lastPos)
{
// found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));

// skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);

// find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}

//let's test it:
int main()
{
string cutMeUp = "The quick brown fox";
vector<string> words;
tokenize( cutMeUp, " ", words );
bool didItWork = false;
if( words.size() == 4 && words[0] == "The" &&
words[1] == "quick" && words[2] == "brown" &&
words[3] == "fox" )
{
didItWork = true;
}
}




Share this post


Link to post
Share on other sites
Hodgman- I recon that would do exactly what the OP is talkin about ;)

I was thinking about this for a project of mine as well... and the code you found is exactly the same as what i have on paper here... amazing what you find on googls ;)

Share this post


Link to post
Share on other sites
The SC++L doesn't have a 'split' function (not that works with string objects, anyway), but Boost does (check out the 'string algorithms' library - you'll find a lot of other useful stuff in there as well).

Share this post


Link to post
Share on other sites

this is a c++/ish way to do it using a stringstream. it demonstrates pretty nicely how c++ is not so hot with intuitive string manipulation. However you could experiment with using vector< string> as a good 'array' container type to hold the strings irrespective of how you code the splitting function.

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>

int main( int argc, char **argv)
{
using namespace std;

stringstream a( "lemonade beer pinacolada");
vector< string> elts;
copy( istream_iterator<string>( a), istream_iterator<string>(), back_inserter( elts));
copy( elts.begin(), elts.end(), ostream_iterator< string>( cout, "\n"));
}


Share this post


Link to post
Share on other sites
thanks all for your inputs i will check them all to see how they work and to study them, to explain better my question and problem is like having a sentence, and wanting to store each word in a variable so that i can later on work with that variable to check against another word, number or anything else

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this