Java - Reverse String
Reverse a string is a common exercise and this is a simple Java solution
The algorithm is:
- Split the String into an array with all the characters
- Reverse the array
- Join the array again
public static String reverse(String s1){
if(isNull(s1)){
return null;
}
List<String> charList = asList(s1.split(""));
Collections.reverse(charList);
return String.join("", charList);
}
Tests:
@Test
public void reverse1(){
String entry = "ABCDEF";
String expected = "FEDCBA";
String result = StringReverser.reverse(entry);
assertEquals(expected, result);
}
@Test
public void reverseNull(){
String entry = null;
String expected = null;
String result = StringReverser.reverse(entry);
assertEquals(expected, result);
}
@Test
public void reverseEmpty(){
String entry = "";
String expected = "";
String result = StringReverser.reverse(entry);
assertEquals(expected, result);
}
Source code: Reverse Class Source code: Reverse Test