normalizeCasingOfSentence function
Processes a sentence and applies appropriate casing rules.
This function takes a sentence string and applies the following rules:
- If most letters are uppercase, converts the entire sentence to uppercase
- Otherwise, capitalizes the first letter and converts the rest to lowercase
Returns the processed sentence with normalized casing.
Implementation
String normalizeCasingOfSentence(final String sentence) {
if (sentence.isEmpty) {
return sentence;
}
StringBuffer result = StringBuffer();
CharacterStats stats = CharacterStats(sentence);
// If most letters in the sentence are uppercase, convert the whole sentence to uppercase
if (stats.mostlyUppercase()) {
return sentence.toUpperCase();
} else {
// Find the first letter in the sentence to capitalize
int firstLetterIndex = sentence
.split('')
.indexWhere((char) => isLetter(char));
if (firstLetterIndex != -1) {
// Capitalize the first letter and lowercase the rest
result.write(sentence.substring(0, firstLetterIndex));
result.write(sentence[firstLetterIndex].toUpperCase());
result.write(sentence.substring(firstLetterIndex + 1).toLowerCase());
} else {
// No letters found, just append the sentence
result.write(sentence);
}
}
return result.toString();
}