Skip to content

Commit b201266

Browse files
Refactored DLL wrapper for improved maintainability & safety.L
1 parent 12ec166 commit b201266

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

CaseConversionAPI/CppLib/src/SentenceCaseConversion.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)