Reverse words String java

Reverse words String java

Write a function to reverse words in a String. Please have java program below which reverse string “Hello World” to “dlroW olleH”:

package com.javahonk;

public class ReverseString {

    public static void main(String[] args) {

        String reverseString = "Hello World";
        char charArray[] = reverseString.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = charArray.length - 1; i >= 0; i--) {
            char c = charArray[i];
            stringBuilder.append(c);
        }

        System.out.println(stringBuilder.toString());

    }

}

Output:

Reverse words String java

Leave a Reply

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