Sunday, May 4, 2014

Reverse the String in different ways


String is a sequence of  characters. It is a java object to store String type of values.String is a immutable class means if we create once String object with value, cannot be change its value. Any java programmer will attend interview,If interviewer asked 10 questions, in that one question is on String related. String is playing vital role in Java language.Recently one of my friend attend one interview, in that his interviewer asked question about String related.What's that question is "In how many ways we can reverse the String?". As per his java String knowledge he told like this "we can reverse the String in 3 different ways.By using String API we can reverse the String in two ways and without using String API we can reverse the String in two ways".He told like this then interviewer asked him explain that 3 ways .He explain all this 3 ways to interviewer.Then he selected in that interview without asking any further question by interviewer.why i should explain my friends interview process here is, how much Java String playing important role in Interviews.Now i will explain that 3 ways with examples.Please see below

1)Generally we can do reverse of String  by using String API method toCharArray(). we call this method on String object then it will return char Array.
                 String stringVar = "Hai how are you man";
            // Here we calling toCharArray() method on  String object  stringVar then arrayForOriginal           // contains array of characters of that String
                 char arrayForOriginal[] = stringVar.toCharArray();
                 int originalArrLen = arrayForOriginal.length;
                 char arrayForReverse[] = new char[originalArrLen];
                 int reverseIndex = 0;
                 for(int indexVal = originalArrLen - 1; indexVal  >= 0; indexVal--) {
                        arrayForReverse[ reverseIndex] =   arrayForOriginal[indexVal] ;
                         reverseIndex++;
                }
               System.out.println( "Reverse of original String is: "+arrayForReverse);  



Output:


         Reverse of original String is:  nam uoy era woh iah 



2)The second way to reverse the String, we can do this little bit different. Means we can perform this way using charAt() method String API. Here we get each character from String and store that character to Another array.After convert that array  to String.
              String stringVar = "Hai how are you man";
           /** Here we calling toCharArray() method on  String object  stringVar then arrayForOriginal          * contains array of characters of that String
         */
                

 int originalArrLen =  stringVar.length;
             String reverseString = "";
                 int reverseIndex = 0;
                for(int i = originalArrLen - 1;i >= 0;i--){
 reverseString  reverseString+stringVar.charAt(i);
}
               System.out.println( "Reverse of original String is: "+ reverseString);  

Output:
            
           Reverse of original String is:  nam uoy era woh iaH 

3) In this way we can do reverse of String without using String API. Means by using StringBuffer Class.Creating StringBuffer object as passing String value as argument to StringBuffer constructor. After calling reverse method of StringBuffer class on StringBuffer object.How we can do this programmatically, see below

           String originalVal = "What are you doing now";

           StringBuffer stringBufferVal = new StringBuffer(originalVal);
          System.out.prinln(stringBufferVal.reverse());
Output:
            
           Reverse of original String is:  nam uoy era woh iaH



See also this posts:


octal to decimal in java

add two numbers each number has more than 1000 numbers
prime number


No comments:

Post a Comment