Skip to content

Commit e17076f

Browse files
author
Geektimus
committed
Add the challenge to reverse a string recursively
1 parent e0d4125 commit e17076f

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ integers. This collection will include the start and end integers if they are od
4141
We need to write a function that will tell us if all the characters on a given string are unique.
4242

4343
### Check permutation
44-
We need to write a function that given two strings let us find if one is a permutation of the other.
44+
We need to write a function that given two strings let us find if one is a permutation of the other.
45+
46+
### Others (Challenges proposed by Friends or Solved on hackatons)
47+
#### Recursively reverse a string
48+
Given a string, reverse the characters and return.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingmaniacs.others;
2+
3+
public class StringChallenges {
4+
/**
5+
* Given a string it reverses the order of the chars
6+
*
7+
* @param str String
8+
* @return String on reverse order
9+
*/
10+
public static String reverse(String str) {
11+
if (str.length() == 0) return str;
12+
else {
13+
int last = str.length() - 1;
14+
return str.charAt(last) + reverse(str.substring(0, last));
15+
}
16+
}
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codingmaniacs.others;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class StringChallengesTests {
8+
9+
@Test
10+
public void reverseRecursiveEmptyString() {
11+
String str = "";
12+
String expected = "";
13+
14+
String result = StringChallenges.reverse(str);
15+
assertEquals("The reverse of an empty string is the same empty string", expected, result);
16+
}
17+
18+
@Test
19+
public void reverseRecursiveSingleCharString() {
20+
String str = "a";
21+
String expected = "a";
22+
23+
String result = StringChallenges.reverse(str);
24+
assertEquals("The reverse of a single char is the same char", expected, result);
25+
}
26+
27+
@Test
28+
public void reverseRecursiveAB() {
29+
String str = "ab";
30+
String expected = "ba";
31+
32+
String result = StringChallenges.reverse(str);
33+
assertEquals("The reverse of ab is ba", expected, result);
34+
}
35+
36+
@Test
37+
public void reverseRecursiveMultipleRepetitions() {
38+
String str = "aaa";
39+
String expected = "aaa";
40+
41+
String result = StringChallenges.reverse(str);
42+
assertEquals("The reverse of aaa is aaa", expected, result);
43+
}
44+
45+
@Test
46+
public void reverseRecursiveComplexString() {
47+
String str = "longer and complex words";
48+
String expected = "sdrow xelpmoc dna regnol";
49+
50+
String result = StringChallenges.reverse(str);
51+
assertEquals("The reverse of an empty string is the same empty string", expected, result);
52+
}
53+
}

0 commit comments

Comments
 (0)