-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChallenge04.java
More file actions
39 lines (30 loc) · 847 Bytes
/
Challenge04.java
File metadata and controls
39 lines (30 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package challenge04;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
/**
* The Class Challenge04.
*
* Reverse String in Java using Iteration and Recursion?
*/
public class Challenge04 {
public static String reverseUsingIteration(String input) {
int len = input.length();
char[] c = new char[len];
len--;
for (int i = 0; i <= len; i++) {
c[i] = input.charAt(len - i);
}
return new String(c);
}
public static String reverse(String input){
if(input.length()<2){
return input;
}
return reverse(input.substring(1)) + input.charAt(0);
}
@Test
public void test() {
assertTrue(Challenge04.reverse("Hello").equals("olleH"));
assertTrue(Challenge04.reverseUsingIteration("Hi Hello How are you").equals("uoy era woH olleH iH"));
}
}