From db5316c764e77d552571edc0d2811901c48d189d Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 1 Jun 2024 16:58:02 +0300 Subject: [PATCH 1/2] created a function for inserting a substring into a string by index --- cstring.h | 32 ++++++++++++++++++++++++++++++++ tests/main.c | 13 +++++++++++++ 2 files changed, 45 insertions(+) diff --git a/cstring.h b/cstring.h index f99bd4c..ec08c52 100644 --- a/cstring.h +++ b/cstring.h @@ -941,4 +941,36 @@ double stringToDouble(TString s) { return number + decimal; } +void stringInsert(TString *s, size_t pos, TString toInsert) { + size_t len_sub = stringLen(toInsert); + size_t len_str = stringLen(*s); + + if (pos > len_str) { + setError(ERR_NAN); + } + + char* new_data = malloc(len_str + len_sub + 1); + if(!new_data){ + setError(ERR_ALLOCATE_SPACE); + } + + clearError(); + + for(size_t i = 0; i < pos; i++){ + new_data[i] = s->data[i]; + } + + for(size_t i = 0; i < len_sub; i++){ + new_data[i + pos] = toInsert.data[i]; + } + + for (size_t i = pos; i < len_str; i++){ + new_data[i + len_sub] = s->data[i]; + } + + free(s->data); + s->data = new_data; + s->size = len_str + len_sub; +} + #endif diff --git a/tests/main.c b/tests/main.c index 92621be..9c85742 100644 --- a/tests/main.c +++ b/tests/main.c @@ -483,6 +483,18 @@ void test_stringToDouble() { printGreen("stringToDouble\n"); } +void test_stringInsert(){ + TString s1 = stringInitWithCharArr("abcdef"); + TString s2 = stringInitWithCharArr("123"); + stringInsert(&s1, 2, s2); + + TString s1expected = stringInitWithCharArr("ab123cdef"); + + assertEq(strncmp(s1.data, s1expected.data, stringLen(s1expected)), 0); + + printGreen("stringInsert\n"); +} + int main() { test_stringStartWith(); test_stringEndWith(); @@ -511,5 +523,6 @@ int main() { test_stringIsPalindrome(); test_stringPad(); test_stringRemove(); + test_stringInsert(); return 0; } From c62d6a104404ecef68d24223ab6b1a5f2f62de8d Mon Sep 17 00:00:00 2001 From: shtykhnoia <144593820+shtykhnoia@users.noreply.github.com> Date: Sun, 2 Jun 2024 17:27:36 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 383d886..60d38ad 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Contributions are what make the open-source community such an amazing place to l - [ ] size_t stringCountSubstring(TString s, TString pattern); - Count occurrences of a substring. - [ ] void stringCapitalize(TString *s); - Capitalize the first letter of each word. - [x] void stringFilter(TString *s, bool (*predicate)(char)); - Remove characters not satisfying a predicate. -- [ ] void stringInsert(TString *s, size_t pos, TString toInsert); - Insert a substring at a specified position. +- [x] void stringInsert(TString *s, size_t pos, TString toInsert); - Insert a substring at a specified position. - [ ] void stringInsertCharArr(TString *s, size_t pos, const char *toInsert); - Insert a substring from a char array. - [x] void stringMap(TString *s, char (*func)(char)); - Apply a function to every character of the string. - [x] void stringMapIndex(TString *s, char (*func)(size_t, char)); - Apply a function to every character of the string with access to its index.