How to reverse a string in java - Java @ Desk

Thursday, May 16, 2013

How to reverse a string in java












public class ReverseString {

    public static void main(String args[]) {
        String string = "My name is kumar";
        StringBuilder reverseString = new StringBuilder();
        for (int i = string.length() - 1; i >= 0; i--) {
            reverseString.append(string.charAt(i));
        }
        System.out.println("Original String : " + string);
        System.out.println("Reverse String : " + reverseString.toString());
    }
}

Output:
Original String : My name is kumar
Reverse String : ramuk si eman yM






No comments:

Post a Comment