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:
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);
}
}