943,982 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11424
  • C++ RSS
Jun 16th, 2007
0

Splitting words from string into character array

Expand Post »
So Im trying to write some code that takes a chunk of text, splits it up into words, then puts these words into an array. What I have is:

C++ Syntax (Toggle Plain Text)
  1. string buf;
  2. stringstream ss(database); // database is my chunk of text
  3.  
  4.  
  5. while (ss >> buf) // Tokenize when whitespace met
  6. {
  7. ???
  8. }

So in that while loop I want to take the string "buf" and put it into an array. Thus the result is an array of strings containing every word in the paragraph. This is where I feel like an idiot.

I have tried a few ways using string streams and converting strings to char arrays etc, but nothing will work for me. I have a feeling that this has to do with multidimensional arrays and pointers, but my searches have been fruitless.

Thanks in advance!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hbweb500 is offline Offline
6 posts
since Feb 2005
Jun 16th, 2007
0

Re: Splitting words from string into character array

My advice, use the vector container. One of the advantages of using C++.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jun 16th, 2007
0

Re: Splitting words from string into character array

C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2. #include <boost/tokenizer.hpp>
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace boost;
  8.  
  9. int main()
  10. {
  11. string str = "The boost tokenizer library provides a flexible "
  12. "and easy to use way to break of a string or other "
  13. "character sequence into a series of tokens.\n"
  14. "Here is a simple example that will break up a "
  15. "paragraph into words.\n";
  16. tokenizer<> toker( str );
  17. vector<string> words( toker.begin(), toker.end() ) ;
  18. cout << "there are " << words.size() << " words\n" ;
  19. cout << "words[6]: " << words[6] << '\n' ;
  20. }
Last edited by vijayan121; Jun 16th, 2007 at 11:29 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 17th, 2007
0

Re: Splitting words from string into character array

What also complicates matters is if your words are separated by more than one space, or even tabs.

In that case, it would probably be wiser to use regular expressions. The task becomes almost trivial.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 18th, 2007
0

Re: Splitting words from string into character array

This is perfect, vijayan, thanks a bunch!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hbweb500 is offline Offline
6 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked lists
Next Thread in C++ Forum Timeline: database and form security





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC