Reverse a String

Write a function that reverses a string in place.


Since we want to reverse this in-place, we place a pointer on either sides and swap both element until we get to the middle. This will take O(n/2) time.

public class Reverse {
	public static void main(String[] args) {

		System.out.println(reverse("Hello world!"));


	static String reverse(String input) {

		char[] charArray = input.toCharArray();

		char temp;

		for (int i = 0, j = charArray.length-1; i < j; i++, j--) {
			temp = charArray[i];
			charArray[i] = charArray[j];
			charArray[j] = temp;

		return String.valueOf(charArray);


Came up with a better solution or have a question? Comment below!