-
Notifications
You must be signed in to change notification settings - Fork 16
Проверяем количество подстрок pattern в строке s. #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -941,4 +941,23 @@ double stringToDouble(TString s) { | |
| return number + decimal; | ||
| } | ||
|
|
||
| size_t stringCountSubstring(TString s, TString pattern) { | ||
| size_t count = 0; | ||
| TString tmp = stringInitWithCharArr(s.data); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется лучше обойтись без лишнего копирования всей строки, есть функция stringCopy(), которая выполняет не глубокое копирование, а просто получает второй указатель на уже существующую строку. Текущая реализация так вообще содержит утечку памяти(( |
||
| size_t pattern_length = pattern.size; | ||
| size_t shift = 0; | ||
|
|
||
| if (pattern_length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| while (stringContains(tmp, pattern)) { | ||
| shift = stringFindFirst(tmp, pattern) + pattern_length; | ||
| count++; | ||
| tmp.data += shift; | ||
| tmp.size -= shift; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -483,6 +483,18 @@ void test_stringToDouble() { | |
| printGreen("stringToDouble\n"); | ||
| } | ||
|
|
||
| void test_stringCountSubstring() { | ||
| TString s1 = stringInitWithCharArr("abc1234dgdgdsfsfdfdsfsfsd1234sfdjg"); | ||
| TString s2 = stringInitWithCharArr("123"); | ||
|
Comment on lines
+487
to
+488
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Добавьте ещё пару тестовых случаев, возможно с вхождениями в начале/конце строки, без вхождений вообще или наоборот только из одинаковых букв. |
||
|
|
||
| size_t result = 2; | ||
| assertEq(stringCountSubstring(s1, s2), result); | ||
| stringDestroy(&s1); | ||
| stringDestroy(&s2); | ||
|
|
||
| printGreen("stringCountSubstring\n"); | ||
| } | ||
|
|
||
| int main() { | ||
| test_stringStartWith(); | ||
| test_stringEndWith(); | ||
|
|
@@ -511,5 +523,6 @@ int main() { | |
| test_stringIsPalindrome(); | ||
| test_stringPad(); | ||
| test_stringRemove(); | ||
| test_stringCountSubstring(); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Добавьте заголовок функции в начале файла, где объявляются все импорты