From bf5a4c68e01326b514b289a81bafce0390c1cfd9 Mon Sep 17 00:00:00 2001 From: gorilli4 <145994444+gorilli4@users.noreply.github.com> Date: Sat, 1 Jun 2024 16:13:40 +0300 Subject: [PATCH 1/2] Update cstring.h --- cstring.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/cstring.h b/cstring.h index f99bd4c..8aee93a 100644 --- a/cstring.h +++ b/cstring.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include typedef enum EErrorCode { @@ -75,6 +77,7 @@ TString stringJoin(TString s1, TString s2, TString delim); TString stringJoinCharArr(TString s1, TString s2, const char *delim); TString stringArrJoin(const TString *s, size_t count, TString delim); TString stringArrJoinCharArr(const TString *s, size_t count, const char *delim); +TString stringInitWithCharArr(const char *str); void stringScan(TString *s); void stringPrint(TString s); @@ -100,6 +103,9 @@ void stringMap(TString *s, char (*func)(char)); void stringMapIndex(TString *s, char (*func)(size_t, char)); void stringRemove(TString *s, size_t pos, size_t len); void stringDestroy(TString *s); +void stringPrint(const TString s); +void stringCapitalize(TString *s); + double stringToDouble(TString s); @@ -941,4 +947,46 @@ double stringToDouble(TString s) { return number + decimal; } -#endif +TString stringInitWithCharArr(const char *str) { + TString s; + s.length = strlen(str); + s.capacity = s.length + 1; + s.data = (char *)malloc(s.capacity * sizeof(char)); + if (s.data != NULL) { + strcpy(s.data, str); + } + return s; +} + +void stringDestroy(TString *s) { + if (s->data != NULL) { + free(s->data); + s->data = NULL; + } + s->length = 0; + s->capacity = 0; +} + +void stringPrint(const TString s) { + if (s.data != NULL) { + printf("%s\n", s.data); + } +} + +void stringCapitalize(TString *s) { + if (s->data == NULL) { + return; + } + + int capitalizeNext = 1; + for (size_t i = 0; i < s->length; i++) { + if (isspace(s->data[i])) { + capitalizeNext = 1; + } else if (capitalizeNext && isalpha(s->data[i])) { + s->data[i] = toupper(s->data[i]); + capitalizeNext = 0; + } else { + s->data[i] = tolower(s->data[i]); + } + } +} From da5e4a4102fbc0d22ed6dd1bd34c04fe3d46dd3e Mon Sep 17 00:00:00 2001 From: gorilli4 <145994444+gorilli4@users.noreply.github.com> Date: Sat, 1 Jun 2024 16:17:06 +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..3640a14 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Contributions are what make the open-source community such an amazing place to l - [ ] int64_t stringLevenshteinDistance(TString s1, TString s2); - Calculate Levenshtein distance between strings. - [x] size_t stringCount(TString s, char c); - Count occurrences of a character. - [ ] 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 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. - [ ] void stringInsertCharArr(TString *s, size_t pos, const char *toInsert); - Insert a substring from a char array.