I wrote this function to format a given sentence via Java, and I'm wondering if there is a better way of doing this without use of external libraries, such as with regex.
public class ChatFormat {
public static void main(String[] args) {
String input = "hello, it's a monday today and i think i'll go to the store. and get? some! tea.";
System.out.println(optimizeText(input));
}
public static String optimizeText(String text) {
char buf[] = text.toLowerCase().toCharArray();
boolean endMarker = true;
for (int i = 0; i < buf.length; i++) {
char c = buf[i];
if (endMarker && c >= 'a' && c <= 'z') {
buf[i] = Character.toUpperCase(c);
endMarker = false;
}
if (c == '.' || c == '!' || c == '?')
endMarker = true;
if (c == 'i') {
char next = 0;
if (i + 1 < buf.length) {
next = buf[i + 1];
}
char last = 0;
if (i - 1 > 0)
last = buf[i - 1];
if (last == ' ' && (next == ' ' || next == '\'' || next == 0))
buf[i] = Character.toUpperCase(c);
}
}
return new String(buf, 0, buf.length);
}
}