Skip to content

Instantly share code, notes, and snippets.

@keng42
Created June 13, 2015 09:07
Show Gist options
  • Save keng42/d583076ecfe8b707cce0 to your computer and use it in GitHub Desktop.
Save keng42/d583076ecfe8b707cce0 to your computer and use it in GitHub Desktop.
Reverse Words in a String
public String reverseWords(String s) {
StringBuffer sb = new StringBuffer();
sb.append("");
String[] arr = s.trim().split("\\s+");
for (int i = arr.length - 1; i > -1; i--) {
sb.append(arr[i]);
if (i != 0) {
sb.append(" ");
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment