Archived

This topic is now archived and is closed to further replies.

How to exctract words from a sentence?

This topic is 4960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Let''s suppose I have something like char a[256]. It''s filled with: "256 + 89", then, after those 8 chars there''s the NULL termination. Now, how can I extract those 3 words into 3 arrays? Those words will always vary in lenght. Is there any useful library function? Or do I have to do it manually? And then, what''s the fastest way? Any help or tips will be greatly appreciated. Thanks -------------- "We cannot all be masters." - William Shakespeare

Share this post


Link to post
Share on other sites
Must this be done in C++?

If not pick up perl and be happy, I bet you''d be able to do it within 3-4 tutorials of perl monks.

Now if you must do it in C++. You could do it via the string class via stl.

Share this post


Link to post
Share on other sites
In C you might consider the use of the strtok() funciton.

In C++, you dump the array into a std::string object and use the find_first_of() function to delimit the string.

boost::tokenizer also does the trick. (In C++)

Share this post


Link to post
Share on other sites
OK. Of course, I didn''t mention the most important.
I''d like to know how to do it in C.
In C++ I''ve already written a nice algoritm which
uses string, stringstream and vector to finish with an array
of arrays of chars.
But I need to do it in C.
I''ve read in many places, that I could use the strtok, but I
have no idea how to use it. The in-array can be any size and it
contain a not static number of words (can be: "2 + 2" or
"45 + 89 \* 324" - it''s for expr, if you know it).
And that''s my problem. I don''t know how to solve it in C only.
Any tutorials on strtok? Are there any at all?
If you could provide me with some examples...


Thanks

--------------
"We cannot all be masters." - William Shakespeare

Share this post


Link to post
Share on other sites
strtok is prolly one of the most evil functions in the std lib. (Apart from the fact that none of the string functions take a length arg)

Unfortunatly, it sounds like it might be your best bet.

The function takes two arguments, the first is the string to tokenize, the second is the list of delimiters.

The first argument is stored in a static variable inside the strtok function. (Told you it was evil) What you need to do is call it once with your string as the 1st argument, this returns the first token. Then call it again with NULL as the first argument. This causes it to search for the next token in the string you sent it last time. Pass it the same set of delimiters each time. Call it like this again and again until it returns NULL to indicate that the end of the string has been reached.

Sorry, that explaination sucked ass but its the best I can do. C++ strings are so much nicer.

Share this post


Link to post
Share on other sites