Write a program to Reverse a String in Java

You can simply use for loop for printing reverse string using charAt(index) method.

public char charAt(int index)
Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Code:
 public class ReverseString {  
      public static void main(String args[])  
      {  
           strReverse("LoveJavaBytes");  
      }  
      public static void strReverse(String str)  
      {  
           int size=str.length();  
           for(int i=size-1;i>=0;i--)  
                System.out.print(str.charAt(i));  
      }  
 }  
Output:
setyBavaJevoL

No comments: