diff --git a/cstring.h b/cstring.h index fc47e7f..b7f8700 100644 --- a/cstring.h +++ b/cstring.h @@ -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 @@ -1008,4 +1011,27 @@ double stringToDouble(TString s) { return number + decimal; } +void stringCapitalize(TString *s) { + 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]); + } + + if (s->data[i] == ' ' || s->data[i] == '\t' || s->data[i] == '\n') { + capitalizeNext = true; + } + } +} + + #endif diff --git a/tests/main.c b/tests/main.c index 835af3c..1f8da75 100644 --- a/tests/main.c +++ b/tests/main.c @@ -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"); @@ -499,6 +500,19 @@ void test_stringToDouble() { printGreen("stringToDouble\n"); } +void test_stringCapitalize() { + TString s = stringInitWithCharArr("hello, world!"); + stringCapitalize(&s); + + TString expected = stringInitWithCharArr("Hello, World!"); + assertEq(stringCompare(s, expected), 0); + + stringDestroy(&s); + stringDestroy(&expected); + printGreen("test_stringCapitalize\n"); +} + + int main() { test_stringStartWith(); test_stringEndWith(); @@ -527,6 +541,7 @@ int main() { test_stringIsPalindrome(); test_stringPad(); test_stringRemove(); - test_stringToInt(); +// test_stringToInt(); + test_stringCapitalize(); // new return 0; }