Files
Java/src/main/java/com/thealgorithms/strings/ReverseString.java
Alex Klymenko 49d1c84cb7 refactor: ReverseString, test improvements (#5406)
* refactor: ReverseString

* refactor: refactor testing into two methods

* checkstyle: fix formatting

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
2024-08-27 13:00:11 +02:00

40 lines
963 B
Java

package com.thealgorithms.strings;
/**
* Reverse String using different version
*/
public final class ReverseString {
private ReverseString() {
}
/**
* easiest way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
/**
* second way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
public static String reverse2(String str) {
if (str == null || str.isEmpty()) {
return str;
}
char[] value = str.toCharArray();
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
char temp = value[i];
value[i] = value[j];
value[j] = temp;
}
return new String(value);
}
}