Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Index String reverse in java

Related Tutorials


 
 

Share on Google+Share on Google+

String reverse in java

Advertisement
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, 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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 14, 2013

Related Tutorials

Discuss: String reverse in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com