Splitting words from string into character array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 4
Reputation: hbweb500 is an unknown quantity at this point 
Solved Threads: 0
hbweb500 hbweb500 is offline Offline
Newbie Poster

Splitting words from string into character array

 
0
  #1
Jun 16th, 2007
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:

  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Splitting words from string into character array

 
0
  #2
Jun 16th, 2007
My advice, use the vector container. One of the advantages of using C++.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Splitting words from string into character array

 
0
  #3
Jun 16th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Splitting words from string into character array

 
0
  #4
Jun 17th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: hbweb500 is an unknown quantity at this point 
Solved Threads: 0
hbweb500 hbweb500 is offline Offline
Newbie Poster

Re: Splitting words from string into character array

 
0
  #5
Jun 18th, 2007
This is perfect, vijayan, thanks a bunch!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC