Created
June 13, 2015 09:07
-
-
Save keng42/d583076ecfe8b707cce0 to your computer and use it in GitHub Desktop.
Reverse Words in a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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