题意:输入一串单词反序的句子,输出单词正序的句子;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String temp = sc.nextLine();// 接收回车符
while (t-- > 0) {
String s = sc.nextLine();
String[] strs = s.split(" ");
temp = "";
for (int i = 0; i < strs.length; i++) {
for (int j = strs[i].length() - 1; j >= 0; j--) {
temp += strs[i].charAt(j);
}
if ((i + 1) == strs.length) {
continue;
}
temp += " ";
}
if (s.endsWith(" ")) {// 接收最后一个空格
temp += " ";
}
System.out.println(temp);
}
}
}
3 olleh !dlrow m‘I morf .udh I ekil .mca
hello world! I‘m from hdu. I like acm.HintRemember to use getchar() to read ‘\n‘ after the interger T, then you may use gets() to read a line and process it.记住用getchar()去接收回车符,然后用 gets() 去读取字符。
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46663625