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
26 changes: 26 additions & 0 deletions cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ void stringMapIndex(TString *s, char (*func)(size_t, char));
void stringRemove(TString *s, size_t pos, size_t len);
void stringDestroy(TString *s);

void stringCapitalize(TString *s);


double stringToDouble(TString s);

#endif
Expand Down Expand Up @@ -1008,4 +1011,27 @@ double stringToDouble(TString s) {
return number + decimal;
}

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 == NULL || s->data == NULL) {
setError(ERR_NULL_POINTER);
return;
}
clearError();

bool capitalizeNext = true;
for (size_t i = 0; i < s->size; ++i) {
if (capitalizeNext && stringCharIsAlpha(s->data[i])) {
s->data[i] = stringCharToUpper(s->data[i]);
capitalizeNext = false;
} else {
s->data[i] = stringCharToLower(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.

Не нужно приводить букву к нижнему регистру. Функция только делает первую букву каждого слова большой (если это буква)

}

if (s->data[i] == ' ' || s->data[i] == '\t' || s->data[i] == '\n') {
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.

В остальном всё хорошо!

capitalizeNext = true;
}
}
}


#endif
23 changes: 19 additions & 4 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,19 @@ void test_stringToInt() {
TString s1 = stringInitWithCharArr("0");
TString s2 = stringInitWithCharArr("-2132456");
TString s3 = stringInitWithCharArr("18446744073709551615");

assertEq(stringToInt(s1), 0);
assertEq(stringToInt(s2), -2132456);
assertEq(stringToInt(s3), 18446744073709551615LL);

stringDestroy(&s1);
stringDestroy(&s2);
stringDestroy(&s3);

printGreen("test_stringToInt\n");
}


void test_stringToDouble() {
TString s1 = stringInitWithCharArr("0");
TString s2 = stringInitWithCharArr("12.34");
Expand All @@ -499,6 +500,19 @@ void test_stringToDouble() {
printGreen("stringToDouble\n");
}

void test_stringCapitalize() {
TString s = stringInitWithCharArr("hello, world!");
stringCapitalize(&s);
Comment on lines +504 to +505
Copy link
Owner

Choose a reason for hiding this comment

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

Добавьте ещё больше разнообразных тестов


TString expected = stringInitWithCharArr("Hello, World!");
assertEq(stringCompare(s, expected), 0);

stringDestroy(&s);
stringDestroy(&expected);
printGreen("test_stringCapitalize\n");
}


int main() {
test_stringStartWith();
test_stringEndWith();
Expand Down Expand Up @@ -527,6 +541,7 @@ int main() {
test_stringIsPalindrome();
test_stringPad();
test_stringRemove();
test_stringToInt();
// test_stringToInt();
Copy link
Owner

Choose a reason for hiding this comment

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

Почему закомментирован старый тест?

test_stringCapitalize(); // new
return 0;
}