forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT58.java
More file actions
31 lines (28 loc) · 892 Bytes
/
Copy pathT58.java
File metadata and controls
31 lines (28 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package books;
/**
* @program JavaBooks
* @description: 翻转字符串
* @author: mf
* @create: 2019/10/08 15:39
*/
/*
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变,
为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student."
则输出"student. a am I"
*/
public class T58 {
public static void main(String[] args) {
String s = "I am a student.";
System.out.println(reverseSentence(s));
}
private static String reverseSentence(String s) {
if (s == null) return null;
if (s.trim().equals("")) return s;
String[] strs = s.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = strs.length - 1; i >= 0; i--) {
sb.append(strs[i]).append(" ");
}
return sb.substring(0, sb.length() - 1);
}
}