Home Reverse String
Post
Cancel

Reverse String

In the following example, reversal string operation is shown in Java language. reverse(s) method completely reverses the string.

public class ReverseString {
	public static void main(String[] args) {
		String s = "Hello world!";
		System.out.println("Reversed String: " + reverse(s));
	}

	public static String reverse(String str) {
        StringBuilder strBuilder = new StringBuilder();
        char[] strChars = str.toCharArray();

        for (int i = strChars.length - 1; i >= 0; i--) {
            strBuilder.append(strChars[i]);
        }

        return strBuilder.toString();
    }
}

Output:

Reversed String: !dlrow olleH
This post is licensed under CC BY 4.0 by the author.