Standard Java. Long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public String reverseWords(String s) {
String[] strs = s.split(" ");
StringBuilder sb = new StringBuilder();
String space = " ";
for (String str : strs) {
sb.append(reverse(str));
sb.append(space);
}
return sb.toString().trim();
}
String reverse(String str) {
char[] arr = str.toCharArray();
int s = 0, e = str.length() - 1;
while (s < e) {
char tmp = arr[s];
arr[s] = arr[e];
arr[e] = tmp;
s++;
e--;
}
return new String(arr);
}

Ruby:

1
2
3
def reverse_words(s)
s.split().map(&:reverse).join(' ')
end

The reverse String method above: standard.

Python:

1
return ' '.join(x[::-1] for x in s.split())

Or : reverse the order of words, then reverse the entire string.

1
return ' '.join(s.split()[::-1])[::-1]