Reverse String in Java: Explanation & Practice

Reverse a string

Problem summary

Write a program that reverses any given string.

Starter code

public class Main {
    public static void main(String[] args) {
        String text = "Hello"; // Test case 1
        
        // Reverse the string
        // Print: Reversed: <result>
    }
}

Expected output and test cases

  • Reverse Hello
    Reversed: olleH
  • Reverse Java
    Reversed: avaJ
  • Single character
    Reversed: a

Hints

  1. Use StringBuilder's reverse() method
  2. Or iterate from end to start
  3. Build new string character by character

Related Strings exercises

Practice all Strings exercises · Run this idea in the Java compiler