Saturday, August 3, 2013

Reverse Function Tweak:


Well ,somebody asked "Can you write a little program of  reversing of a String without using StringBuffer or StringBuilder" ? Being a developer i said  hmmm, why not ,it's seems to be easy.
Started coding (we accept challenge instantaneously ), and believe me it was not. 

There are two ways that i am aware of  :
1)Simply use for loop and it's done(That was easy )

reverseString("Hello");//what we would do without "Hello"

public static void reverseString(String source){
if(source == null || source.length() == 0){
System.out.println("Source is Empty");
return;
}
char [] reverseArray = new char[source.length()];
for(int index = 0 ; index < reverseArray.length  ;index++){

reverseArray[index] = source.charAt(source.length() -1 - index);
        }
System.out.println(new String(reverseArray));

}

2) Use recursive function (Damn man, there is always another way).

System.out.println(recursiveReverseString("I love my Marantz amplifier with KEF Speakers"));//Really I do ;)

public static String  recursiveReverseString(String source){
   //This print statement will give us insight.
   System.out.println("***********" + source);
//Every recursive call should have a proper termination //call,otherwise prepare for java stackoverflowerror 
   if(source == null || source.length() <2){
System.out.println("Returning from here and source is       " + source);
return source;
 }
   return recursiveReverseString
(source.substring(1)) + source.charAt(0);
//substring always returns new String

}


I know last statement marked in yellow was little overdose.Printing statement will make it clear.
Here is print statement : 

***********I love my Marantz amplifier with KEF Speakers
*********** love my Marantz amplifier with KEF Speakers
***********love my Marantz amplifier with KEF Speakers
***********ove my Marantz amplifier with KEF Speakers
***********ve my Marantz amplifier with KEF Speakers
***********e my Marantz amplifier with KEF Speakers
*********** my Marantz amplifier with KEF Speakers
***********my Marantz amplifier with KEF Speakers
***********y Marantz amplifier with KEF Speakers
*********** Marantz amplifier with KEF Speakers
***********Marantz amplifier with KEF Speakers
***********arantz amplifier with KEF Speakers
***********rantz amplifier with KEF Speakers
***********antz amplifier with KEF Speakers
***********ntz amplifier with KEF Speakers
***********tz amplifier with KEF Speakers
***********z amplifier with KEF Speakers
*********** amplifier with KEF Speakers
***********amplifier with KEF Speakers
***********mplifier with KEF Speakers
***********plifier with KEF Speakers
***********lifier with KEF Speakers
***********ifier with KEF Speakers
***********fier with KEF Speakers
***********ier with KEF Speakers
***********er with KEF Speakers
***********r with KEF Speakers
*********** with KEF Speakers
***********with KEF Speakers
***********ith KEF Speakers
***********th KEF Speakers
***********h KEF Speakers
*********** KEF Speakers
***********KEF Speakers
***********EF Speakers
***********F Speakers
*********** Speakers
***********Speakers
***********peakers
***********eakers
***********akers
***********kers
***********ers
***********rs
***********s
Returning from here and source is       s

srekaepS FEK htiw reifilpma ztnaraM ym evol I


Which implementation I will go for? Well it depends which side of chair I am sitting on ,means weather I am taking an interview or giving one.

Couple of days back , I faced an issue with recursive call where i was navigating through millions of records , JVM didn't like it and threw stackoverflowerror.

Happy Tweaking!!!!









No comments:

Post a Comment