Complete the code to reverse a string

Report a typo

Consider a simple utility program in Java meant to reverse a given string. Some critical parts of this program are missing, denoted by the '▭'. Your task is to fill in the blanks in the code to ensure it correctly reverses and prints out the given input string 'Hello, world!' The input string is not going to change.

Fill in the gaps with the relevant elements
public class Main {
    public static void main(String[] args) {
        String input = "Hello, world!";
        String reversed = "";
        for (int i = input.() - 1; i >= 0; i--) {
            reversed += input.(i);
        }
        System.out.println(reversed);
    }
}
charAtlength

Create a free account to access the full topic