From 596792431f1e6f68e51644b6e80a5b652c6e9742 Mon Sep 17 00:00:00 2001 From: Denis Borisov Date: Tue, 21 May 2024 22:01:02 +0300 Subject: [PATCH] implementation of stringCapitalize and testing --- cstring.h | 13 +++++++++++++ tests/main.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/cstring.h b/cstring.h index 1a76fc5..88332ad 100644 --- a/cstring.h +++ b/cstring.h @@ -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 @@ -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') { + s->data[i] = stringCharToUpper(s->data[i]); + } + } +} + #endif diff --git a/tests/main.c b/tests/main.c index fe2513c..95bb105 100644 --- a/tests/main.c +++ b/tests/main.c @@ -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)); + 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(); @@ -498,6 +532,7 @@ int main() { test_stringIsPalindrome(); test_stringPad(); test_stringRemove(); + test_stringCapitalize(); return 0; }