Thursday 20 November 2014

Java program to reverse a string

Java program to reverse a string
Hi all , Today I am going to write a post on very popular interview program which is How to Reverse a string in Java .
As String interview question have always been a favorite of almost all interviewers there’s always been a program on String manipulation in an interview .String programs vary from searching a particular word  in a sentence or a file ,reverse a string ,Number of times a particular word or character appears in a sentence or file and many more .

So today I am going to discus how to reverse a String in java .This program also helps to find that whether a String is Palindrome or not .

A palindrome String can be checked by comparing the original String with the reverse of that String with the help of  equals() method or “==” .If both are equal then that means the String is palindrome .But for that first you need to know about how to reverse a String .
The reverse of a String example I have done it in two ways.
  1. Reverse of String without using reverse method or Reverse a String using CharAt() method or Reverse a String without using String Buffer .
  2. Reverse of String using reverse method or Reverse a String without using String Buffer .
If you know both of the methods to reverse a String then you can impress the interviewer by writing both the methods J

So , Here is the Example of  Reverse of String without using reverse method  or Reverse a String using CharAt() method or Reverse a String without using String Buffer .

 
 
import java.util.*;
class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
 
      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);
   }
}
 
 
 
Explanation:
1. In the above Example we are asking the user to enter a String to reverse.
2. After calculating the length of the String we are using a loop to get each character from end of String ( as we are starting the loop form end) and then appending the characters using + operator .

This is quite simple example .Hope you all understand.

Now Comes the second method of Revere a String.
Reverse of String using reverse method or Reverse a String without using String Buffer .

 
import java.util.*;
class InvertString
{
   public static void main(String args[])
   {
      StringBuffer a = new StringBuffer("Java programming is fun");
      System.out.println(a.reverse());
   }
}
 

In this example we are using StringBuffer and its reverse method to reverse a string .

If you want input from user in StringBuffer case then you can use the program below :

 
import java.util.*;
class ReverseString
{
   public static void main(String args[])
   {
      String original ;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
 
      
      StringBuffer a = new StringBuffer(original);
      System.out.println(a.reverse());
 
   }
}




So this was my post on How to Reverse a String in Java .If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)

You might interested in :

Program to print Fibonacci series in Java 

No comments:

Post a Comment