File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ Given a string, break it into a vector of substrings separated by a specific
2
+ character. For example: get the string "name.midname.lastname" and the
3
+ character "." as separator, the function should return a list/vector with the
4
+ strings {"name", "midname", "lastname"}
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < string>
4
+ using namespace std ;
5
+
6
+ /* * Gets a string and separe through a separator _sep and returns into a
7
+ * std::vector<std::string> */
8
+ std::vector<std::string> split (std::string _str, char _sep) {
9
+ std::vector<std::string> v;
10
+ int begin = 0 ;
11
+ for (int i=0 ; i < _str.size () ; i++){
12
+ if (_str[i] == _sep) {
13
+ v.push_back ( _str.substr (begin, i-begin) );
14
+ begin = i+1 ;
15
+ }
16
+ }
17
+ v.push_back (_str.substr (begin, _str.size ()));
18
+ return v;
19
+ };
20
+
21
+
22
+ int main () {
23
+
24
+ string str1 = " name.midname.lastname" ;
25
+ vector<string> vecStr1 = split (str1, ' .' );
26
+ for (string& s : vecStr1) {
27
+ cout << s << endl;
28
+ }
29
+
30
+ cout << endl;
31
+ string str2 =
" [email protected] " ;
32
+
33
+ vector<string> vecStr2 = split (str2, ' @' );
34
+ for (string& s : vecStr2) {
35
+ cout << s << endl;
36
+ }
37
+
38
+ return 0 ;
39
+ }
You can’t perform that action at this time.
0 commit comments