File tree Expand file tree Collapse file tree
CaseConversionAPI/CppLib/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,9 +35,26 @@ std::string SentenceCaseConversion::convert(const std::string& input) const {
3535 LowerCaseConversion lowerConv;
3636 UpperCaseConversion upperConv;
3737
38+ // Step 1: convert entire string to lowercase
3839 std::string result = lowerConv.convert (input);
39- std::string first (1 , result[0 ]);
40- result[0 ] = upperConv.convert (first)[0 ];
40+
41+ bool capitalizeNext = true ;
42+
43+ for (size_t i = 0 ; i < result.size (); ++i) {
44+ char & c = result[i];
45+
46+ if (std::isalpha (static_cast <unsigned char >(c))) {
47+ if (capitalizeNext) {
48+ std::string temp (1 , c);
49+ c = upperConv.convert (temp)[0 ];
50+ capitalizeNext = false ;
51+ }
52+ }
53+
54+ if (c == ' .' || c == ' !' || c == ' ?' ) {
55+ capitalizeNext = true ;
56+ }
57+ }
4158
4259 return result;
43- }
60+ }
You can’t perform that action at this time.
0 commit comments