From 081f308b9d7c2a22935eff6f8b7b18258111a3d3 Mon Sep 17 00:00:00 2001 From: Suchi Bansal Date: Fri, 6 Oct 2023 23:45:27 +0530 Subject: [PATCH] Add `ReverseWordsInString` (#4456) * return a string of the words in reverse order concatenated by a single space. Input: s = "the sky is blue" Output: "blue is sky the" * return a string of the words in reverse order concatenated by a single space. Input: s = "the sky is blue" Output: "blue is sky the" * space reduce * removed main method * added test cases * formatting fix * formatting fix * worked on pr reviews * formatting fix * private constructor added * added test case for when string contains white space * simplified method * fix issue * formatting issues fix * fixed issue * code refactor * documented method * worked on pr comments * docs: add missing space * tests: express as `ParameterizedTest` --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> Co-authored-by: vil02 --- .../strings/ReverseWordsInString.java | 22 +++++++++++++++++++ .../strings/ReverseWordsInStringTest.java | 20 +++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/ReverseWordsInString.java create mode 100644 src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java diff --git a/src/main/java/com/thealgorithms/strings/ReverseWordsInString.java b/src/main/java/com/thealgorithms/strings/ReverseWordsInString.java new file mode 100644 index 000000000..5f9d27b4e --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ReverseWordsInString.java @@ -0,0 +1,22 @@ +package com.thealgorithms.strings; + +import java.util.Arrays; +import java.util.Collections; + +public final class ReverseWordsInString { + + private ReverseWordsInString() { + } + + /** + * @brief Reverses words in the input string + * @param s the input string + * @return A string created by reversing the order of the words in {@code s} + */ + + public static String reverseWordsInString(final String s) { + var words = s.trim().split("\\s+"); + Collections.reverse(Arrays.asList(words)); + return String.join(" ", words); + } +} diff --git a/src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java b/src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java new file mode 100644 index 000000000..44e397459 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java @@ -0,0 +1,20 @@ +package com.thealgorithms.strings; + +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class ReverseWordsInStringTest { + @ParameterizedTest + @MethodSource("inputStream") + void numberTests(String expected, String input) { + Assertions.assertEquals(expected, ReverseWordsInString.reverseWordsInString(input)); + } + + private static Stream inputStream() { + return Stream.of(Arguments.of("blue is Sky", "Sky is blue"), Arguments.of("blue is Sky", "Sky \n is \t \n blue "), Arguments.of("", ""), Arguments.of("", " "), Arguments.of("", "\t")); + } +}