| | |
string manipulation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer? 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
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






