Skip to content

Commit 841ca94

Browse files
committed
Add reverse string snippet
1 parent 56118df commit 841ca94

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

src/main/java/Library.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/test/java/LibraryTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)