Skip to content

Commit

Permalink
added count_str
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanExtreme002 committed Jun 14, 2024
1 parent 1c627fa commit 2f7b20e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions fastsnake/templates/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ vector<string> split(const string &str, char delimiter = ' ') {
return tokens;
}

string toLowerCase(string &str) {
string to_lower(string &str) {
char new_str[str.length() + 1];

for (int i = 0; i < str.length(); i++) {
Expand All @@ -66,7 +66,7 @@ string toLowerCase(string &str) {
return string(new_str);
}

string toUpperCase(string &str) {
string to_upper(string &str) {
char new_str[str.length() + 1];

for (int i = 0; i < str.length(); i++) {
Expand All @@ -76,7 +76,7 @@ string toUpperCase(string &str) {
new_str[str.length()] = '\0';
return string(new_str);
}

string replace_str(string str, string sub_string, string new_string, int n = -1) {
while (n--) {
int pos = str.find(sub_string);
Expand All @@ -89,6 +89,17 @@ string replace_str(string str, string sub_string, string new_string, int n = -1)
return str;
}

int count_str(string &str, string target) {
int occurrences = 0;
string::size_type pos = 0;

while ((pos = str.find(target, pos )) != string::npos) {
occurrences += 1;
pos += target.length();
}
return occurrences;
}

// [Slice Functions]: ==========================================================
template <typename T> vector<T> slice(vector<T> &vec, int start, int end, int step = 1) {
if (step == 0 || (step > 0 && start >= end) || (step < 0 && start <= end)) {
Expand Down

0 comments on commit 2f7b20e

Please sign in to comment.