getSentenceCaseText()
return a string representation of current text in sentence case. Sentence case is the conventional way of using capital letters in a sentence or capitalizing only the first word and any proper nouns. In addition, all capital word should remain as it is.
For this assignment, noun is limited to words that have ONE capital letter at the beginning.
**As an example the string "First SenTence. secOND sentence. tHIRD SENTENCE"
its output will be (First sentence. Second sentence. Third SENTENCE)**
This is my code for the above assignment. I could capitalize the first letter after every dot and set the rest as lowercase but i couldn't find out how to keep full uppercase word as it is.
This is my code below:
public String getSentenceCaseText(String text) {
int pos = 0;
boolean capitalize = true;
StringBuilder sb = new StringBuilder(text);
while (pos < sb.length()){
sb.setCharAt(pos, Character.toLowerCase(sb.charAt(pos)));
if (sb.charAt(pos) == '.') {
capitalize = true;
} else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) {
sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos)));
capitalize = false;
}
pos++;
}
return sb.toString();
}