Write a Java program to reverse a String using iterative method

We can use the method toCharArray() returns an Array of chars after converting a String into sequence of characters. The returned array length is equal to the length of the String and the sequence of chars in Array matches the sequence of characters in the String.

public class MyString
{
    public static String reverseString(String str)
    {
        // Convert to char array
        char ch[]=str.toCharArray();  
        
        // To store the reversed string
        String rev="";  
 
        // Iterate from last of character array
        for(int i=ch.length-1;i>=0;i--)
        {  
           // Concatenate character to rev string one by one
            rev += ch[i];  
        }  
        // Return the final reversed string after coming out of loop
        return rev;  
    } 

    public static void main(String []args)
    {
        System.out.println(MyString.reverseString("Hello"));
    }    
}

Output: olleH