String Reverse Program

This java program reverse a string. We use charAt method to extract characters from the string and append them in reverse order to reverse the string.

 

 


public class ReverseString {
public static void main(String args[])
{
       String original = "vision", reverse = "";
       int length = original.length();

        for ( int i = length - 1 ; i >= 0 ; i-- ){
	    reverse = reverse + original.charAt(i);
        }
                                
        System.out.println("Reverse of entered string is: "+reverse);
}
}

 

Output : Reverse of entered string is:noisiv

 

Reverse string using StringBuffer class:

 


public class ReverseString
{
   public static void main(String args[])
   {
      StringBuffer a = new StringBuffer("vision");
      System.out.println(a.reverse());
   }
}	

 

Output : noisiv

 

Using toCharArray function:


	public class ReverseString{
	public static void main(String args[]){
	String s = "abcdef";
	char c[] = s.toCharArray();
		
	for( int i = c.length -1; i>=0; i--)
	System.out.print(c[i]);
	}
}	

Output : fedcba