diff --git a/cstring.h b/cstring.h index 1a76fc5..b5ac9c0 100644 --- a/cstring.h +++ b/cstring.h @@ -102,6 +102,8 @@ 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 // for testing: @@ -910,5 +912,47 @@ void stringDestroy(TString *s) { *s = (TString) {0}; } +bool charIsSep(char cur_ch){ + char sep[5] = {' ', '\t', '\n', ',', '.'}; + for (int i=0; i<5; i++){ + if (cur_ch == sep[i]) return true; + } + return false; +} + +void stringCapitalize(TString *s){ + typedef enum Tstate{ + start, + end + } Tstate; + Tstate state = start; + size_t len = s->size; + for(size_t idx=0; idxdata[idx]; + switch(state){ + case start: + if ( charIsSep(symb) ){ + state = start; + } + else{ + if ( stringCharIsAlpha(symb)){ + stringCharToUpper(symb); + state = end; + } else state = end; + } + break; + case end: + if (charIsSep(symb)){ + state = start; + } else state = end; + break; + + default: + state = end; + break; + } + } +} + #endif diff --git a/tests/main.c b/tests/main.c index fe2513c..a00005a 100644 --- a/tests/main.c +++ b/tests/main.c @@ -471,6 +471,27 @@ void test_stringRemove() { printGreen("test_stringRemove\n"); } +void test_stringCapitalize(){ + TString s1 = stringInitWithCharArr("Hello, World! testing remove"); + TString res1 = stringInitWithCharArr("Hello, World! Testing Remove"); + stringCapitalize(&s1); + assertEq(stringCompare(s1, res1), 0); + TString s2 = stringInitWithCharArr("1ello, World! testing remove"); + TString res2 = stringInitWithCharArr("1ello, World! Testing Remove"); + stringCapitalize(&s2); + assertEq(stringCompare(s2, res2), 0); + TString s3 = stringInitWithCharArr("Hello, World! tes&ting remove"); + TString res3= stringInitWithCharArr("Hello, World! Tes&ting Remove"); + stringCapitalize(&s3); + assertEq(stringCompare(s3, res3), 0); + TString s4 = stringInitWithCharArr("Hello, World!,testing remove"); + TString res4 = stringInitWithCharArr("Hello, World!,Testing Remove"); + stringCapitalize(&s4); + assertEq(stringCompare(s4, res4), 0); + + printGreen("test_stringCapitalize\n"); +} + int main() { test_stringStartWith(); test_stringEndWith(); @@ -498,6 +519,7 @@ int main() { test_stringIsPalindrome(); test_stringPad(); test_stringRemove(); + test_stringCapitalize(); return 0; }