-
Notifications
You must be signed in to change notification settings - Fork 20k
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
vil02
merged 25 commits into
TheAlgorithms:master
from
Suchiq:suchi-reverse-word-in-string
Oct 6, 2023
Merged
Add ReverseWordsInString
#4456
Changes from 22 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 5658b7f
return a string of the words in reverse order concatenated by a singl…
Suchiq 7934837
Merge branch 'master' into suchi-reverse-word-in-string
Suchiq 4ac4044
space reduce
Suchiq b0b5798
Merge branch 'suchi-reverse-word-in-string' of https://github.com/Suc…
Suchiq 781d0f0
removed main method
Suchiq 9f04fbd
added test cases
Suchiq 73c6778
formatting fix
Suchiq 5e84c5f
formatting fix
Suchiq 614a899
worked on pr reviews
Suchiq 5ee6cf0
formatting fix
Suchiq e8cce5d
private constructor added
Suchiq 43191a7
added test case for when string contains white space
Suchiq 9182386
simplified method
Suchiq 12509e6
Merge branch 'master' into suchi-reverse-word-in-string
Suchiq 72fc5f1
fix issue
Suchiq e055191
Merge branch 'suchi-reverse-word-in-string' of https://github.com/Suc…
Suchiq f7d4ee4
formatting issues fix
Suchiq b38b347
fixed issue
Suchiq c7b5adc
code refactor
Suchiq 6a406f5
documented method
Suchiq bec0c45
worked on pr comments
Suchiq 4ee75ab
docs: add missing space
vil02 6bdd7d9
tests: express as `ParameterizedTest`
vil02 ecf4e1d
Merge branch 'master' into suchi-reverse-word-in-string
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/com/thealgorithms/strings/ReverseWordsInString.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var words = s.trim().split("\\s+"); | ||
Collections.reverse(Arrays.asList(words)); | ||
return String.join(" ", words); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("")); | ||
} | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.