题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student. a am I”。
看着没啥技术含量..注意细节就好,两个指针,每次记录一个单词,在循环中调整正确的指向..
public class Q10 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] array = str.toCharArray();
String result = "";
int low = array.length - 1;
int high = low;
while(low >=0 )
{
while(low >= 0 && array[low]!=' ')
low--;
if(low == -1)
{
for(int i = 0; i <= high;i++)
result = result + array[i];
break;
}
else
{
for(int i = low+1; i <= high;i++)
result = result + array[i];
}
while(low>=0&&array[low]==' ')
{
low--;
result += " ";
}
high = low;
}
System.out.println(result);
}
}