ReverseWords { public static String reverseWords(String s) { char[] cs = s.toCharArray(); int start = -1; for (int i = 0; i < cs.length; i++) { if (start == -1 && cs[i] != ' ') { start = i; } if ((i == cs.length - 1 || cs[i+1] == ' ') && start != -1) { reverse(cs, start, i + 1); start = -1; } } reverse(cs, 0, cs.length); return new String(cs); }  static String reverseWords(String s);  }
@Test(dataProvider = "dp") public void test(String s, String r) { assertEquals(ReverseWords.reverseWords(s), r); }