package com.basic;

public class ReverseWordsInString {

	public static String reverseWords(String s) {
		if (s == null || s.length() == 0) {
			return "";
		}
		StringBuilder rst = new StringBuilder();
		char[] arr = s.toCharArray();
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == ' ') {
				rst.append(arr[i]);
			} else {
				int j = i;
				StringBuilder tmp = new StringBuilder();
				while (j < arr.length && Character.isLetter(arr[j])) {
					tmp.append(arr[j]);
					j++;
				}
				tmp.reverse();
				rst.append(tmp);
				i = j - 1;
			}
		}
		return rst.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(reverseWords(" i ekil siht"));

	}

}

@@ -611,16 +611,649 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09// use char array since string is immutable%0D%0A%09public String reverseWords2(String words) %7B%0D%0A%09%09if (words == null  %7C%7C words.length() == 0) %7B%0D%0A%09%09%09return %22%22;%0D%0A%09%09%7D%0D%0A%09%09char%5B%5D s = words.toCharArray();%0D%0A%09%09reverse(s, 0, s.length - 1);%0D%0A%09%09System.out.println(String.valueOf(s));%0D%0A%09%09for (int i = 0, j = 0; j %3C= s.length; j ++) %7B%0D%0A%09%09%09if (j == s.length %7C%7C s%5Bj%5D == ' ') %7B%0D%0A%09%09%09%09reverse(s, i, j-1);%0D%0A%09%09%09%09i = j + 1;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return String.valueOf(s);%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09private void reverse(char%5B%5D s, int begin, int end) %7B%0D%0A%09%09while ( begin %3C end) %7B%0D%0A%09%09%09char tmp = s%5Bbegin%5D;%0D%0A%09%09%09s%5Bbegin%5D = s%5Bend%5D;%0D%0A%09%09%09s%5Bend%5D = tmp;%0D%0A%09%09%09begin++;%0D%0A%09%09%09end--;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public 

Update ReverseWordsInString.java