Thursday, November 2, 2017

String Reverse in Java




package com.String.Reverse;

public class ReverseString {

public static void main(String[] args) {

String s = "Hello world";
StringBuilder sb = new StringBuilder(s);
System.out.println(s);
System.out.println(sb.reverse());
}
}


OUTPUT : Hello world
                   dlrow olleH
---------------------------------------------------------------------------------------------

package com.String.Reverse;

public class StringRevers {

public static void main(String[] args) {

String str = "Hello World";
StringBuffer strBuffer = new StringBuffer(str);
StringBuilder strBuilder = new StringBuilder(str);

System.out.println("String : "+strBuffer);
System.out.println("With StringBuffer : "+strBuffer.reverse());
System.out.println("With StringBuider : "+strBuilder.reverse());


}

}
OUTPUT : String : Hello World
                   With StringBuffer : dlroW olleH
                   With StringBuider : dlroW olleH


----------------------------------------------------------------------------------------------

Reversing String with the help of "Char Array".

package com.String.Reverse;

public class ReverseStringWithChar {

public static void main(String[] args) {

//String str = "The verge";
String str = "egrev ehT";
char[] chr = str.toCharArray();

for(int i=chr.length-1; i>=0; i--){

System.out.print(chr[i]);
}

System.out.println();
}

}


OUTPUT : The verge
                   egrev ehT
--------------------------------------------------------------------------------------------------------------

Reverse String 

package com.String.Reverse;

public class ReverseString {

public static void main(String args[]) {

String str = "Hello World";
char[] chr = str.toCharArray();

for (int i = chr.length-1; i >=0; i--) {
System.out.print(chr[i]);
}
System.out.println();
}
}

OUTPUT:

dlroW olleH
-----------------------------------------------------------------------------------------------------------


Reverse String with StringBuilder

package com.String.Reverse;

public class ReverseWithBuilder {
public static void main(String args[]){
String str = "Hello World";
StringBuilder strbld = new StringBuilder(new String(str));
strbld = strbld.reverse();
System.out.println(strbld);
}
}

OUTPUT:

dlroW olleH

-----------------------------------------------------------------------------------------------------------------

Reverse String with TempArray


package com.String.Reverse;

public class ReverseStringTemp {

public static void main(String[] args) {

String str = "Hello World";
char[] temparray = str.toCharArray();
int left, right=0;
right = temparray.length-1;
for(left=0; left<right ; left++, right--){
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp; 
}
for(char C : temparray){
System.out.print(C);
}
System.out.println();
}

}

OUTPUT:

dlroW olleH
------------------------------------------------------------------------------------------------

Reverse String with ArrayList

package com.String.Reverse;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class Reverse {

public static void main(String[] args) {

String input = "Hello World";
char[] hello = input.toCharArray();
List<Character> trial1 = new ArrayList<>();

for (char c : hello)
trial1.add(c);

Collections.reverse(trial1);
ListIterator li = trial1.listIterator();
while (li.hasNext()) {
System.out.print(li.next());
}
System.out.println();
}

}

OUTPUT:

dlroW olleH
------------------------------------------------------------------------------------------------------------
 Reverse a String using Recursion
package com.String.Reverse;
public class JavaExampleRecursive {
public static void main(String[] args) {
String str = "Hello World";
        String reversed = reverseString(str);
        System.out.println("The reversed string is: " + reversed);
}
public static String reverseString(String str)
    {
        if (str.isEmpty())
            return str;
        //Calling Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}
OUTPUT:

The reversed string is: dlroW olleH

-------------------------------------------------------------------------------
 Reverse a String using Recursion-2

package com.String.Reverse;

public class StringReverseRecurssion {

public static void main(String[] args) {
String str = "Hello World";
StringReverseRecurssion obj = new StringReverseRecurssion();
obj.reverse(str);
}

void reverse(String str) {
if ((str == null) || (str.length() <= 1))
System.out.println(str);
else {
System.out.print(str.charAt(str.length() - 1));
reverse(str.substring(0, str.length() - 1));
}
}

}

OUTPUT:

dlroW olleH
----------------------------------------------------------------------------------------------------
Reverse a String using Recursion-ALL

package com.String.Reverse;

public class StringReverseExampleAll {

public static void main(String args[])  {

        //original string
        String str = "Hello World";
        System.out.println("Original String: " + str);

        //reversed string using Stringbuffer
        String reverseStr = new StringBuffer(str).reverse().toString();
        System.out.println("Reverse String in Java using StringBuffer: " + reverseStr);

        //iterative method to reverse String in Java
        reverseStr = reverse(str);
        System.out.println("Reverse String in Java using Iteration: " + reverseStr);

        //recursive method to reverse String in Java
        reverseStr = reverseRecursively(str);
        System.out.println("Reverse String in Java using Recursion: " + reverseStr);

    }

    public static String reverse(String str) {
        StringBuilder strBuilder = new StringBuilder();
        char[] strChars = str.toCharArray();

        for (int i = strChars.length - 1; i >= 0; i--) {
            strBuilder.append(strChars[i]);
        }

        return strBuilder.toString();
    }

    public static String reverseRecursively(String str) {

        //base case to handle one char string and empty string
        if (str.length() < 2) {
            return str;
        }

        return reverseRecursively(str.substring(1)) + str.charAt(0);

    }
}

OUTPUT:
Original String: Hello World Reverse String in Java using StringBuffer: dlroW olleH Reverse String in Java using Iteration: dlroW olleH Reverse String in Java using Recursion: dlroW olleH




----------------------------------------------------------------------------------------------

No comments:

Post a Comment