Write a Java program to reverse each word in a given string.
Java program to Reverse words
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class Strings {
private static final Pattern PATTERN = Pattern.compile(" +");
private static final String WHITESPACE = " ";
private Strings() {
throw new AssertionError("Cannot be instantiated");
}
public static String reverseWordsV1(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return "";
}
String[] words = str.split(WHITESPACE);
StringBuilder reversedString = new StringBuilder();
for (String word : words) {
StringBuilder reverseWord = new StringBuilder();
for (int i = word.length() - 1; i >= 0; i--) {
reverseWord.append(word.charAt(i));
}
reversedString.append(reverseWord).append(WHITESPACE);
}
return reversedString.toString();
}
public static String reverseWordsV2(String str) {
// or throw IllegalArgumentException
if (str == null || str.isBlank()) {
return "";
}
return PATTERN.splitAsStream(str)
.map(w -> new StringBuilder(w).reverse())
.collect(Collectors.joining(WHITESPACE));
}
public static String reverse(String str) {
// or throw IllegalArgumentException
if (str == null || str.isBlank()) {
return "";
}
return new StringBuilder(str).reverse().toString();
}
}
Test
import java.util.concurrent.TimeUnit;
public class Main {
private static final String TEXT = " Java is my fav programming language. I love Java coding";
public static void main(String[] args) {
System.out.println("Input text: \n" + TEXT + "\n");
System.out.println("Reverse words in String via StringBuilder:");
long startTimeV1 = System.nanoTime();
String reversedV1 = Strings.reverseWordsV1(TEXT);
displayExecutionTime(System.nanoTime() - startTimeV1);
System.out.println("Reversed: \n" + reversedV1);
System.out.println();
System.out.println("Reverse words in String using Java 8 functional-style:");
long startTimeV2 = System.nanoTime();
String reversedV2 = Strings.reverseWordsV2(TEXT);
displayExecutionTime(System.nanoTime() - startTimeV2);
System.out.println("Reversed: \n" + reversedV2);
System.out.println();
System.out.println("Reverse String via StringBuilder:");
long startTimeV3 = System.nanoTime();
String reversedV3 = Strings.reverse(TEXT);
displayExecutionTime(System.nanoTime() - startTimeV3);
System.out.println("Reversed: \n" + reversedV3);
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ns" + " ("
+ TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS) + " ms)");
}
}
Output:
Input text:
Java is my fav programming language. I love Java coding
Reverse words in String via StringBuilder:
Execution time: 49344800 ns (49 ms)
Reversed:
avaJ si ym vaf gnimmargorp .egaugnal I evol avaJ gnidoc
Reverse words in String using Java 8 functional-style:
Execution time: 23643400 ns (23 ms)
Reversed:
avaJ si ym vaf gnimmargorp .egaugnal I evol avaJ gnidoc
Reverse String via StringBuilder:
Execution time: 31600 ns (0 ms)
Reversed:
gnidoc avaJ evol I .egaugnal gnimmargorp vaf ym si avaJ
Comments
Post a Comment