From a2b1d19c2ea5836ea6e70f47bf0634f50af63703 Mon Sep 17 00:00:00 2001 From: Tyhyqo Date: Sat, 1 Jun 2024 17:16:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=BE=20=D0=BF=D0=BE=D0=B4=D1=81=D1=82=D1=80=D0=BE=D0=BA?= =?UTF-8?q?=20pattern=20=D0=B2=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B5=20s.?= =?UTF-8?q?=20=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=B4=D0=B8=D0=BC=20count=20-?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82,=20tm?= =?UTF-8?q?p=20-=20=D0=BA=D0=BE=D0=BF=D0=B8=D1=8E=20s=20pattern=5Flengt?= =?UTF-8?q?=D1=80=20-=20=D0=B4=D0=BB=D0=B8=D0=BD=D1=83=20pattern.=20=D0=9F?= =?UTF-8?q?=D0=BE=D0=BA=D0=B0=20tmp=20=D1=81=D0=BE=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=B8=D1=82=20pattern,=20=D1=83=D0=B2=D0=B5=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=B8=D0=B2=D0=B0=D0=B5=D0=BC=20=D1=81=D1=87=D0=B5=D1=82=D1=87?= =?UTF-8?q?=D0=B8=D0=BA=20=D0=BD=D0=B0=201,=20=D0=B0=20tmp=20=D1=81=D0=B4?= =?UTF-8?q?=D0=B2=D0=B8=D0=B3=D0=B0=D0=B5=D0=BC=20=D0=B2=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B4=20=D0=BD=D0=B0=20shift=20(=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BC=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8C=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=B2=D1=85=D0=BE=D0=B6=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D0=B4=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8),=20=D1=83=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B0?= =?UTF-8?q?=D0=B5=D0=BC=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=20tmp=20?= =?UTF-8?q?=D0=BD=D0=B0=20shift.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cstring.h | 19 +++++++++++++++++++ tests/main.c | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cstring.h b/cstring.h index f99bd4c..2f6790d 100644 --- a/cstring.h +++ b/cstring.h @@ -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); + 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 diff --git a/tests/main.c b/tests/main.c index 92621be..22a2c41 100644 --- a/tests/main.c +++ b/tests/main.c @@ -483,6 +483,18 @@ void test_stringToDouble() { printGreen("stringToDouble\n"); } +void test_stringCountSubstring() { + TString s1 = stringInitWithCharArr("abc1234dgdgdsfsfdfdsfsfsd1234sfdjg"); + TString s2 = stringInitWithCharArr("123"); + + 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; }