Java Program for Reverse of String

By | 2018-03-21T21:53:04+00:00 June 23rd, 2016|java programs|0 Comments

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

Happy Learning 🙂

About the Author:

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+ ,

Leave A Comment