I am a student 中国
中国 student a am I
先将每个单词反转,再将整体字符串反转
public class ReverseWords {
private static String word;
public ReverseWords(String str){
this.word=str;
}
public void reverse(){
int length=word.length();
int begin=-1,end=-1;
for(int i=0;i
if(begin==-1&&word.charAt(i)==' ')//字符串开头有空格
continue;
if(begin==-1){//确定单词首位置
begin=i;
continue;
}
if(word.charAt(i)==' '){//确定单词尾
end=i-1;
}
else if (i==length-1) {//遍历到字符串末尾
end=i;
}
else {
continue;
}
reverse(begin,end);
begin=-1;
end=-1;
}
reverse(0,length-1);
}
public void