Monday , September 25 2017
Home / java programs / Java Program for Reverse of String

Java Program for Reverse of String

Here we are going to write a program for Reverse of String in Java.

Reverse of String Example:


import java.util.Scanner;

public class StringReverse {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Any Word : ");
        String input = sc.nextLine();
        System.out.println("Reverse is : "+reverse(input));
    }

    public static String reverse(String input) {
        String reverse = null;
        char[] data = null;
        try {
            if (input != null) {
                data = input.toCharArray();

                int start = 0;
                int end = input.length() - 1;

                while (start < end) {
                    data[start] ^= data[end];
                    data[end] ^= data[start];
                    data[start] ^= data[end];
                    ++start;
                    --end;
                }
                reverse = String.valueOf(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            data = null;
        }
        return reverse;
    }
}

Output :

Enter Any Word : onlinetutorialspoint
Reverse is : tniopslairotutenilno

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java program to find sum of digits

In this Java programming tutorials, I am going to show you how to calculate sum …

Leave a Reply

Your email address will not be published. Required fields are marked *