Splitting a string into tokens using strtok and string as parameter

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

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Splitting a string into tokens using strtok and string as parameter

 
0
  #1
22 Days Ago
I have got an example for cplusplus.com for converting a string into a char array so i can split in into tokens and adds to a vector and it works fine in the main method but if i try and create a separate function and pass a string as a parameter, the program crashes with no explanation. It compiles fine.

Have i created the function type correctly? I want to create a vector of strings so i can output each vector.

  1. // strings and c-strings
  2. #include <iostream>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> SplitString (string aString)
  10. {
  11. vector<string> vec;
  12. char * cstr, *p;
  13.  
  14. //string str ("Please split this phrase into tokens");
  15.  
  16. string str = aString;
  17.  
  18. cstr = new char [str.size()+1];
  19. strcpy (cstr, str.c_str());
  20.  
  21. // cstr now contains a c-string copy of str
  22.  
  23. p=strtok (cstr," ");
  24. while (p!=NULL)
  25. {
  26. vec.push_back(p);
  27. p=strtok(NULL," ");
  28. }
  29.  
  30. delete[] cstr;
  31. }
  32.  
  33. int main ()
  34. {
  35. std::vector<std::string> vec = SplitString("Please split this phrase into tokens");
  36.  
  37. for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
  38. {
  39. cout << vec.at(a) << '\n';
  40. }
  41. return 0;
  42.  
  43. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
1
  #2
22 Days Ago
  1. // strings and c-strings
  2. #include <iostream>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> SplitString (string aString)
  10. {
  11. vector<string> vec;
  12. char * cstr, *p;
  13.  
  14. //string str ("Please split this phrase into tokens");
  15.  
  16. string str = aString;
  17.  
  18. cstr = new char [str.size()+1];
  19. strcpy (cstr, str.c_str());
  20.  
  21. // cstr now contains a c-string copy of str
  22.  
  23. p=strtok (cstr," ");
  24. while (p!=NULL)
  25. {
  26. vec.push_back(p);
  27. p=strtok(NULL," ");
  28. }
  29.  
  30. delete[] cstr;
  31. return vec;
  32. }
  33.  
  34. int main ()
  35. {
  36. std::vector<std::string> vec = SplitString("Please split this phrase into tokens");
  37.  
  38. for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
  39. {
  40. cout << vec.at(a) << '\n';
  41. }
  42.  
  43. cin.get();
  44. return 0;
  45.  
  46. }
Last edited by iamthwee; 22 Days Ago at 12:29 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

Tags
strtok

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC