string manipulation

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

string manipulation

 
0
  #1
Oct 18th, 2009
Is there any functions that split a string?

Such as this:

  1. string s1 = "lol-lol2-lol3";
  2. string s2[3];
  3. char spliter_char = '-';
  4.  
  5. split_strings(s1, s2, spliter_char);
  6. //s2 should now be:
  7. //{"lol","lol2","lol3"}
...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 251
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 18th, 2009
You can use the substr() function to copy part of a string to another string.
I find this website really good for stuff like this here.
That is a link to the substr() reference but there is a whole lot of examples for other class functions.

Added an example program I made for an assignment that takes in a sentence and stores all the words into a vector. You can use this example and instead of looking for a space character look for the '-'.
Last edited by sfuo; Oct 18th, 2009 at 3:25 am. Reason: Misread
Attached Files
File Type: cpp a2.cpp (1.8 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,457
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #3
Oct 18th, 2009
Not tested.

  1. bool split_string(string& src, string& dst, char splitter)
  2. {
  3. //check for valid size or if splitter is in src
  4. if(src.size() == 0 || src.find(splitter) == string::npos)
  5. return false;
  6. int i = 0;
  7. while(src[i] != splitter)
  8. dst += src[i++];
  9.  
  10. return true;
  11. }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz
 
0
  #4
Oct 18th, 2009
What i mean is to have to dst thing a char* array.

I want it to do this:

  1. string str = "10-20-30";
  2. char* buffer[3];
  3. split_string(str, buffer, '-');
  4. //buffer should now be {"10","20","30"}
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 10
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training
 
0
  #5
Oct 18th, 2009
Havn't tested this, so there might be a few errors:

  1. void split_string(const string& src, vector<string>& dst, char split)
  2. {
  3. int startPos = 0, endPos = src.find(split);
  4. while(endPos != string::npos)
  5. {
  6. dst.push_back(src.substr(startPos, endPos-startPos));
  7. startPos = endPos + 1;
  8. endPos = src.find(split, startPos);
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 251
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #6
Oct 18th, 2009
I posted the answer in my attachment and you ended up just reposting what you put up in your 1st post.

I took my code and deleted a whole bunch of stuff and changed the find character from ' ' to '-'. This stores everything to a vector but you can change it to a char* array[3] if you want to but I'll leave you to think that part up.

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string sentence, word, charString, wordString;
  10. vector<string>wordlist;
  11.  
  12. cout << "Please enter some words (ctrl-d or ctrl-z to quit): " << endl;
  13.  
  14. while(getline(cin, sentence))
  15. {
  16. wordlist.clear();
  17. int lastSpace = -1;
  18.  
  19. for( int i = 0; i < sentence.length(); i++ )
  20. {
  21. if( sentence[i] == '-' )
  22. {
  23. word = sentence.substr(lastSpace+1, (i-lastSpace-1));
  24. wordlist.push_back(word);
  25. lastSpace = i;
  26. }
  27. else if( i == sentence.length()-1 )
  28. {
  29. word = sentence.substr(lastSpace+1, (i-lastSpace));
  30. wordlist.push_back(word);
  31. }
  32. }
  33.  
  34. if( wordlist.size() > 0 )
  35. {
  36. for(int i = 0; i < wordlist.size(); i++)
  37. {
  38. cout << wordlist[i] << endl;
  39. }
  40. }
  41. else
  42. {
  43. cout << "You didn't enter any words." << endl << endl;
  44. }
  45.  
  46. }
  47. system("PAUSE");
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 351 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC