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

One thought on “Reverse words String java

  1. We can achieve the reverse string by simply using the function reverse() :
    public class HelloWorld
    {
    // arguments are passed using the text field below this editor
    public static void main(String[] args)
    {
    String input = “Hello World”;
    String output = new StringBuilder(input).reverse().toString();
    System.out.println(output);
    }
    }

Leave a Reply

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

I am not Robot *