package pers.lyt.java;
public class Offer58_ReverseWordsInSentence {
public String ReverseSentence(char[] chars) {
if (chars == null || chars.length <= 0)
return String.valueOf(chars);
reverseSb(chars, 0, chars.length - 1);
int start = 0;
int end = 0;
while (start < chars.length) {
while (end < chars.length && chars[end] != ' ')
end++;
reverseSb(chars, start, end - 1);
start = ++end;
}
return String.valueOf(chars);
}
private void reverseSb(char[] chars, int start, int end) {
while (start < end) {
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
Offer58_ReverseWordsInSentence demo = new Offer58_ReverseWordsInSentence();
char[] cLetter = new char[]{'I',' ','a','m',' ','a',' ','s','t','u','d','e','n','t','.'};
String sentence = demo.ReverseSentence(cLetter);
System.out.println(sentence);
}
}