Tuesday, 3 January 2017

Write a Java Program to Reverse String ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class ReverseString {
 
//Using For Loop 
 public static void usingLoop(String str){
  String reverse = "";
  for(int i=str.length()-1;i>=0;i--){
   reverse+= str.charAt(i);
  }
  System.out.println("REVERSE USING LOOP : "+reverse);
 }

 //Using String Buffer
 public static void usingStringBuffer(String str){
  StringBuffer sb = new StringBuffer(str);
  sb.reverse();
  System.out.println("REVERSE USING STRING BUFFER : "+sb);
 }

 //Using Recursive Method 
 public static String usingRecursion(String str){
  if(str == null || str.length() <= 1 ){
   return str;
  }
  return usingRecursion(str.substring(1))+str.charAt(0);
 }

 public static void main(String[] args) {
  ReverseString.usingLoop("pushkar");
  ReverseString.usingStringBuffer("pushkar");
  System.out.println("USING RECURSIVE METHOD "+ReverseString.usingRecursion("pushkar"));
 }
}

Output : 

REVERSE USING LOOP : rakhsup
REVERSE USING STRING BUFFER : rakhsup
USING RECURSIVE METHOD : rakhsup



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment