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
13 changes: 13 additions & 0 deletions cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ 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 stringCapitalize(TString *s);

#endif

Expand Down Expand Up @@ -910,5 +911,17 @@ void stringDestroy(TString *s) {
*s = (TString) {0};
}

void stringCapitalize(TString *s) {
if (s == NULL || s->size == 0) return;
if ('a' <= s->data[0] && s->data[0] <= 'z') {
s->data[0] = stringCharToUpper(s->data[0]);
}
for (size_t i = 1; i < s->size; ++i) {
if (s->data[i - 1] == ' ' && 'a' <= s->data[i] && s->data[i] <= 'z') {
Copy link
Owner

Choose a reason for hiding this comment

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

Everything is good.
The separators can be one of ' ', '\n', '\t', ',', '.'
Make a separate function for check if symbol is a separator

s->data[i] = stringCharToUpper(s->data[i]);
}
}
}


#endif
35 changes: 35 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,40 @@ void test_stringRemove() {
printGreen("test_stringRemove\n");
}

void test_stringCapitalize() {
TString simpleTString = stringInitWithCharArr("hello World! testing capitalize");
TString spaceThenDigitsTString = stringInitWithCharArr(" 123hello world! ");
TString specialSignThenDigitsTestString = stringInitWithCharArr("!123hello world! 0testing capitalize");

TString expectedSimpleTString = stringInitWithCharArr("Hello World! Testing Capitalize");
TString expectedSpaceThenDigitsTString = stringInitWithCharArr(" 123hello World! ");
TString expectedSpecialSignThenDigitsString = stringInitWithCharArr("!123hello World! 0testing Capitalize");

stringCapitalize(&simpleTString);
stringCapitalize(&spaceThenDigitsTString);
stringCapitalize(&specialSignThenDigitsTestString);

assertEq(stringLen(simpleTString), stringLen(expectedSimpleTString));
Copy link
Owner

Choose a reason for hiding this comment

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

Is it necessary to check the length? There is a function

assertEq(strncmp(simpleTString.data, expectedSimpleTString.data, simpleTString.size), 0);

assertEq(stringLen(spaceThenDigitsTString), stringLen(expectedSpaceThenDigitsTString));
assertEq(strncmp(
spaceThenDigitsTString.data,
expectedSpaceThenDigitsTString.data,
spaceThenDigitsTString.size),
0);

assertEq(stringLen(specialSignThenDigitsTestString), stringLen(expectedSpecialSignThenDigitsString));
assertEq(strncmp(
specialSignThenDigitsTestString.data,
expectedSpecialSignThenDigitsString.data,
specialSignThenDigitsTestString.size),
0);

printGreen("test_stringCapitalize");
}


int main() {
test_stringStartWith();
test_stringEndWith();
Expand Down Expand Up @@ -498,6 +532,7 @@ int main() {
test_stringIsPalindrome();
test_stringPad();
test_stringRemove();
test_stringCapitalize();
return 0;
}

Expand Down