Skip to content

Commit d118798

Browse files
committed
Added Split a String Question
1 parent ef7360d commit d118798

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

SplitString/Question.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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"}

SplitString/Solution.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)