Skip to content

Add ReverseWordsInString #4456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
21b5ade
return a string of the words in reverse order concatenated by a singl…
Suchiq Sep 22, 2023
5658b7f
return a string of the words in reverse order concatenated by a singl…
Suchiq Sep 22, 2023
7934837
Merge branch 'master' into suchi-reverse-word-in-string
Suchiq Sep 30, 2023
4ac4044
space reduce
Suchiq Sep 30, 2023
b0b5798
Merge branch 'suchi-reverse-word-in-string' of https://github.com/Suc…
Suchiq Sep 30, 2023
781d0f0
removed main method
Suchiq Oct 1, 2023
9f04fbd
added test cases
Suchiq Oct 1, 2023
73c6778
formatting fix
Suchiq Oct 1, 2023
5e84c5f
formatting fix
Suchiq Oct 1, 2023
614a899
worked on pr reviews
Suchiq Oct 2, 2023
5ee6cf0
formatting fix
Suchiq Oct 2, 2023
e8cce5d
private constructor added
Suchiq Oct 2, 2023
43191a7
added test case for when string contains white space
Suchiq Oct 2, 2023
9182386
simplified method
Suchiq Oct 2, 2023
12509e6
Merge branch 'master' into suchi-reverse-word-in-string
Suchiq Oct 2, 2023
72fc5f1
fix issue
Suchiq Oct 3, 2023
e055191
Merge branch 'suchi-reverse-word-in-string' of https://github.com/Suc…
Suchiq Oct 3, 2023
f7d4ee4
formatting issues fix
Suchiq Oct 3, 2023
b38b347
fixed issue
Suchiq Oct 3, 2023
c7b5adc
code refactor
Suchiq Oct 5, 2023
6a406f5
documented method
Suchiq Oct 6, 2023
bec0c45
worked on pr comments
Suchiq Oct 6, 2023
4ee75ab
docs: add missing space
vil02 Oct 6, 2023
6bdd7d9
tests: express as `ParameterizedTest`
vil02 Oct 6, 2023
ecf4e1d
Merge branch 'master' into suchi-reverse-word-in-string
vil02 Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thealgorithms.strings;

import java.util.Arrays;
import java.util.Collections;


public final class ReverseWordsInString {

private ReverseWordsInString() {
}

/**
* Reverse word in the string
*
* @param s contains sentence
@return Reverse word of {@code s}
*/

public static String reverseWordsInString(final String s) {
var words = s.trim().split("\\s+");
Collections.reverse(Arrays.asList(words));
return String.join(" ", words);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ReverseWordsInStringTest {

@Test
public void testCorrectReverseWordsInTheString() {
assertEquals("blue is Sky", ReverseWordsInString.reverseWordsInString("Sky is blue"));
}

@Test
public void testCorrectReverseWordsInTheStringWithWhiteSpace() {
assertEquals("blue is Sky", ReverseWordsInString.reverseWordsInString("Sky \n is \n \n blue"));
}

@Test
public void testReverseWordsInStringForEmpty() {
assertEquals("", ReverseWordsInString.reverseWordsInString(""));
}
}