Repository: TheAlgorithms/Java
Pull request #6182 (2025-02-26 15:06:55)

feat: add reverseUsingStringBuilder method to reverse a string
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6182?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 74.11%. Comparing base [(`63ce6b8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/63ce6b8ca57e99203dbc5129a50662d5cc48799e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9ffc962`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9ffc96258dc238118113f489bcf867b7da9da04d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 8 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6182      +/-   ##
============================================
+ Coverage     73.97%   74.11%   +0.14%     
- Complexity     5157     5194      +37     
============================================
  Files           662      663       +1     
  Lines         17681    17778      +97     
  Branches       3407     3423      +16     
============================================
+ Hits          13079    13177      +98     
  Misses         4097     4097              
+ Partials        505      504       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6182?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

geetoor-maven: > Please put this in a separate class with an appropritate name

Thank you for your feedback! I've moved the method to a separate class with an appropriate name as suggested. Let me know if any further adjustments are needed

Patch:
diff --git a/src/main/java/com/thealgorithms/strings/ReverseString.java b/src/main/java/com/thealgorithms/strings/ReverseString.java
index 46a0494f..54a9b779 100644
--- a/src/main/java/com/thealgorithms/strings/ReverseString.java
+++ b/src/main/java/com/thealgorithms/strings/ReverseString.java
@@ -36,4 +36,25 @@ public final class ReverseString {
         }
         return new String(value);
     }
+
+    /**
+     * Reverse version 3 the given string using a StringBuilder.
+     * This method converts the string to a character array,
+     * iterates through it in reverse order, and appends each character
+     * to a StringBuilder.
+     *
+     * @param string The input string to be reversed.
+     * @return The reversed string.
+     */
+    public static String reverse3(String string) {
+        if (string.isEmpty()) {
+            return string;
+        }
+        char[] chars = string.toCharArray();
+        StringBuilder sb = new StringBuilder();
+        for (int i = string.length() - 1; i >= 0; i--) {
+            sb.append(chars[i]);
+        }
+        return sb.toString();
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/ReverseStringTest.java b/src/test/java/com/thealgorithms/strings/ReverseStringTest.java
index 501f7029..08f5fb58 100644
--- a/src/test/java/com/thealgorithms/strings/ReverseStringTest.java
+++ b/src/test/java/com/thealgorithms/strings/ReverseStringTest.java
@@ -25,4 +25,10 @@ public class ReverseStringTest {
     public void testReverseString2(String input, String expectedOutput) {
         assertEquals(expectedOutput, ReverseString.reverse2(input));
     }
+
+    @ParameterizedTest
+    @MethodSource("testCases")
+    public void testReverseString3(String input, String expectedOutput) {
+        assertEquals(expectedOutput, ReverseString.reverse3(input));
+    }
 }