Skip to content

Commit 0a0435e

Browse files
authored
Merge pull request #14 from saminjay/cpp-string-split
Added string split snippet in CPP
2 parents 62a9240 + 69e4ef2 commit 0a0435e

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

public/data/cpp.json

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,39 @@
66
"title": "Reverse String",
77
"description": "Reverses the characters in a string.",
88
"code": [
9-
"string reverseString(const string& input) {",
10-
" string reversed = input;",
11-
" reverse(reversed.begin(), reversed.end()); // Using STL's reverse function",
9+
"#include <string>",
10+
"#include <algorithm>",
11+
"",
12+
"std::string reverseString(const std::string& input) {",
13+
" std::string reversed = input;",
14+
" std::reverse(reversed.begin(), reversed.end());",
1215
" return reversed;",
1316
"}"
1417
],
1518
"tags": ["cpp", "array", "reverse", "utility"],
1619
"author": "Vaibhav-kesarwani"
20+
},
21+
{
22+
"title": "Split String",
23+
"description": "Splits a string by a delimiter",
24+
"code": [
25+
"#include <string>",
26+
"#include <vector>",
27+
"",
28+
"std::vector<std::string> split_string(std::string str, std::string delim) {",
29+
" std::vector<std::string> splits;",
30+
" int i = 0, j;",
31+
" int inc = delim.length();",
32+
" while (j != std::string::npos) {",
33+
" j = str.find(delim, i);",
34+
" splits.push_back(str.substr(i, j - i));",
35+
" i = j + inc;",
36+
" }",
37+
" return splits;",
38+
"}"
39+
],
40+
"tags": ["cpp", "string", "split", "utility"],
41+
"author": "saminjay"
1742
}
1843
]
1944
}

0 commit comments

Comments
 (0)