10000 Create string_example.cpp · JeeeunOh/basic-algo-lecture@02747ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 02747ff

Browse files
Create string_example.cpp
1 parent 6d6168d commit 02747ff

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

0x1E/string_example.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<string> split(string s, string sep){
5+
vector<string> ret;
6+
int pos = 0;
7+
while(pos < s.size()){
8+
int nxt_pos = s.find(sep, pos);
9+
if(nxt_pos == -1) nxt_pos = s.size();
10+
if(nxt_pos - pos > 0)
11+
ret.push_back(s.substr(pos, nxt_pos - pos));
12+
pos = nxt_pos + sep.size();
13+
}
14+
return ret;
15+
}
16+
17+
int main(){
18+
string s = "hello";
19+
s += " BKD!"; // hello BKD!
20+
cout << s.size() << '\n'; // 10
21+
cout << s.substr(2, 3) << '\n'; // llo
22+
cout << s[1] << '\n'; // e
23+
s.replace(6, 4, "guys"); // hello guys
24+
cout << s << '\n';
25+
int it = s.find("guys"); // 6
26+
s.replace(it, 4, "everyone"); // hello everyone
27+
cout << s << '\n';
28+
s.erase(7, 6); // hello ee
29+
cout << s << '\n';
30+
s[6] = 'm'; // hello me
31+
cout << s << '\n';
32+
s.insert(0, "say ");// say hello me
33+
cout << s << '\n';
34+
if(s.find("to") == string::npos) // string::npos == -1
35+
cout << "'to' is not in string 's'\n";
36+
vector<string> chunks1 = split("welcome to the black parade", " ");
37+
// welcome/to/the/black/parade/
38+
for(auto chunk : chunks1) cout << chunk << '/';
39+
cout << '\n';
40+
vector<string> chunks2 = split("b*!*ac*!*e*!*y*!*", "*!*");
41+
// b/ac/a/y/
42+
for(auto chunk : chunks2) cout << chunk << '/';
43+
}

0 commit comments

Comments
 (0)
0