File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ package MustDoEasyList ;
2+
3+ public class ReverseWordsInAStringIII557 {
4+
5+ public static String reverseWords (String s ) {
6+ StringBuilder ans = new StringBuilder ();
7+
8+ int count = 0 ;
9+ for (String str : s .split (" " )) {
10+ String currReversed = reverseOneWord (str );
11+ if (count == 0 )
12+ ans .append (currReversed );
13+ else {
14+ ans .append (" " );
15+ ans .append (currReversed );
16+ }
17+ count ++;
18+ }
19+
20+ return ans .toString ();
21+
22+ }
23+
24+ private static String reverseOneWord (String word ) {
25+ StringBuilder ans = new StringBuilder ();
26+ for (int i = word .length () - 1 ; i >= 0 ; i --) {
27+ char ch = word .charAt (i );
28+ ans .append (ch );
29+ }
30+ return ans .toString ();
31+ }
32+
33+ public static void main (String [] args ) {
34+ System .out .println (reverseWords ("hello world" ));
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments