Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

- [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.
Expand Down
50 changes: 49 additions & 1 deletion cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


typedef enum EErrorCode {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Такой print нельзя, так как буффер строки не заканчивается \0

}
}

Comment on lines +950 to +975
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Но эти функции же уже есть в коде библиотеки

void stringCapitalize(TString *s) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chat-GPT ❤️

if (s->data == NULL) {
return;
}

int capitalizeNext = 1;
for (size_t i = 0; i < s->length; i++) {
if (isspace(s->data[i])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И добавьте знаки пунктуации, которые тоже разделяют слова

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]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не нужно уменьшать буквы, функция только делает большими первые буквы

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readme says: void stringCapitalize(TString *s); - Capitalize the first letter of each word.

}
}
}