| | |
string manipulation
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Is there any functions that split a string?
Such as this:
Such as this:
c++ Syntax (Toggle Plain Text)
string s1 = "lol-lol2-lol3"; string s2[3]; char spliter_char = '-'; split_strings(s1, s2, spliter_char); //s2 should now be: //{"lol","lol2","lol3"}
...
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 '-'.
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
0
#3 Oct 18th, 2009
Not tested.
C++ Syntax (Toggle Plain Text)
bool split_string(string& src, string& dst, char splitter) { //check for valid size or if splitter is in src if(src.size() == 0 || src.find(splitter) == string::npos) return false; int i = 0; while(src[i] != splitter) dst += src[i++]; return true; }
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
0
#4 Oct 18th, 2009
What i mean is to have to dst thing a char* array.
I want it to do this:
I want it to do this:
C++ Syntax (Toggle Plain Text)
string str = "10-20-30"; char* buffer[3]; split_string(str, buffer, '-'); //buffer should now be {"10","20","30"}
...
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 10
0
#5 Oct 18th, 2009
Havn't tested this, so there might be a few errors:
CPP Syntax (Toggle Plain Text)
void split_string(const string& src, vector<string>& dst, char split) { int startPos = 0, endPos = src.find(split); while(endPos != string::npos) { dst.push_back(src.substr(startPos, endPos-startPos)); startPos = endPos + 1; endPos = src.find(split, startPos); } }
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.
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string sentence, word, charString, wordString; vector<string>wordlist; cout << "Please enter some words (ctrl-d or ctrl-z to quit): " << endl; while(getline(cin, sentence)) { wordlist.clear(); int lastSpace = -1; for( int i = 0; i < sentence.length(); i++ ) { if( sentence[i] == '-' ) { word = sentence.substr(lastSpace+1, (i-lastSpace-1)); wordlist.push_back(word); lastSpace = i; } else if( i == sentence.length()-1 ) { word = sentence.substr(lastSpace+1, (i-lastSpace)); wordlist.push_back(word); } } if( wordlist.size() > 0 ) { for(int i = 0; i < wordlist.size(); i++) { cout << wordlist[i] << endl; } } else { cout << "You didn't enter any words." << endl << endl; } } system("PAUSE"); return 0; }
![]() |
Similar Threads
- Need Help in String Manipulation? (C#)
- String Manipulation (Python)
- Jr C Developer(Entry level) with string manipulation required (Tech / IT Consultant Job Offers)
- string manipulation functions? (C++)
- Help Please - with String Manipulation (C++)
- string manipulation (C)
- C String Manipulation (C)
Other Threads in the C++ Forum
- Previous Thread: Debug
- Next Thread: Floating point exception
Views: 351 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






