Java - Reverse String - alternate
This is an alternate solution for reversing Strings in java
The algorithm:
- Split the String into an array with all the characters
- Iterate throught the array, starting from the last position and adding into a new string
public static String reverse(String s1) {
if(isNull(s1)){
return null;
}
StringBuilder stringBuilder = new StringBuilder();
String[] splitted = s1.split("");
for (int i = splitted.length - 1; i >= 0; i--) {
stringBuilder.append(splitted[i]);
}
return stringBuilder.toString();
}
Tests:
@Test
public void reverse1(){
String entry = "ABCDEF";
String expected = "FEDCBA";
String result = StringReverserByConcat.reverse(entry);
assertEquals(expected, result);
}
@Test
public void reverseNull(){
String entry = null;
String expected = null;
String result = StringReverserByConcat.reverse(entry);
assertEquals(expected, result);
}
@Test
public void reverseEmpty(){
String entry = "";
String expected = "";
String result = StringReverserByConcat.reverse(entry);
assertEquals(expected, result);
}
Source code: Reverse Class Source code: Reverse Test