String reverse in java

In this section we are going to discuss about how to reverse a sting in java. There are many ways to reverse a string in java. Java API provides StringBuffer and StringBuilder reverse() method which can easily reverse a string.

String reverse in java

In this section we are going to discuss about how to reverse a sting in java. There are many ways to reverse a string in java. Java API provides StringBuffer and StringBuilder reverse() method which can easily reverse a string.

String reverse in java

String reverse in java

In this section we are going to discuss about how to reverse a sting in java. There are many ways to reverse a string in java  java API provides StringBuffer and StringBuilder  reverse() method which can easily reverse a string, this is the most easy way to reverse a string in java but in most interview they will ask to reverse a string without StringBuffer and StringBuilder methods. For that we will do it by recursive way as well as by loop.  Now here is the example how to reverse a string in java.

public class Stringreverse 
      {
         public static void main(String args[]) 
          {
        String word1=" Welcome to Rose India";
        String reverse="";
     //Using String Buffer 
         System.out.println("Using String Buffer ");
         String reverses = new StringBuffer(word1).reverse().toString();
        System.out.println("Original String is = " +word1);
        System.out.println("Reverse String is = "+reverses);
        String word2 = "Hello everybody";
        System.out.println();

     //Using String Builder 
         System.out.println("Using String Builder ");
        reverse = new StringBuilder(word2).reverse().toString();
        System.out.println("Original String is = " +word2);
        System.out.println("Reverse String is = "+reverse); 
        System.out.println();
        
     //Using Iterative by loop
        System.out.println("Using Loop ");
        String word3="Rose India";
        String rev="";
        for(int i = word3.length() -1; i>=0; i--)
         {
            rev = rev + word3.charAt(i);
         }
         System.out.println("Original String is = " +word3);
         System.out.println("Reverse String is = "+rev);
         }
      }

Output : After compiling and executing of above program.

Download SourceCode