Reverse String in Java
Date: Feb 2014
Level: Basic


INTERVIEW QUESTIONS

Question Title: Reverse String in Java
Language: Java
QUESTION DETAILS:

Given a string, return the string with the words reversed
For example:
"I am good" -> "good am I"


Here's the code:
public class StringReverse {
	String str = "";
	public StringReverse(String str){
		this.str = str;
	}
	public String reverse() {
		String[] strings = str.split(" ");
		StringBuilder sb = new StringBuilder();
		for (int i = strings.length-1; i >=0 ; i--) {
			sb.append(strings[i] + " ");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		StringReverse sr = new StringReverse("I am in the river");
		String str = sr.reverse();
		System.out.println(str);
	}
	
}
All Questions