File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed
Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ Update the sample application with the snippet and add a test for it. After prov
1717* [ Factorial] ( #factorial )
1818* [ Fibonacci] ( #fibonacci )
1919
20+ ### String
21+ * [ Reverse string] ( #reverse-string )
22+
2023## Array
2124
2225### Generic two array concatenation
@@ -77,3 +80,15 @@ Update the sample application with the snippet and add a test for it. After prov
7780```
7881
7982[ ⬆ back to top] ( #table-of-contents )
83+
84+ ## String
85+
86+ ### Reverse string
87+
88+ ``` java
89+ public static String reverseString(String s) {
90+ return new StringBuilder (s). reverse(). toString();
91+ }
92+ ```
93+
94+ [ ⬆ back to top] ( #table-of-contents )
Original file line number Diff line number Diff line change @@ -65,4 +65,13 @@ public static int factorial(int number) {
6565 }
6666 return result ;
6767 }
68+
69+ /**
70+ * Reverse string
71+ * @param s the string to reverse
72+ * @return reversed string
73+ */
74+ public static String reverseString (String s ) {
75+ return new StringBuilder (s ).reverse ().toString ();
76+ }
6877}
Original file line number Diff line number Diff line change @@ -74,4 +74,9 @@ public void testFactorial() {
7474 assertEquals (362880 , Library .factorial (9 ));
7575 assertEquals (3628800 , Library .factorial (10 ));
7676 }
77+ @ Test
78+ public void testReverseString () {
79+ assertEquals ("oof" , Library .reverseString ("foo" ));
80+ assertEquals ("ÖÄÅ321FED cba" , Library .reverseString ("abc DEF123ÅÄÖ" ));
81+ }
7782}
You can’t perform that action at this time.
0 commit comments